Please enable JavaScript.
Coggle requires JavaScript to display documents.
Java Fundamentals (Objects (String is an Object (Two ways of creating…
Java Fundamentals
Objects
An object is a instance of a class
Objects can have multiple references
instance variables
instance of an object
instance methods
String is an Object
Two ways of creating strings
String is an array of chars
String objects are immutable
creating String from chars, string to chars
convert int, bool, long to string
StringBuilder class
Scanner Class
Comparing String values
Local Object
NumberFormat object
Date & Time object
Date
GregorianCalendar
DateFormat
Java 8: LocalDate, LocalDateTime, DateTimeFormatter
Comparison of old date & new Java 8 date
Creating Custom Classes
Encapsulate data & method
Breaking functionality into small, maintainable unit
Grouping functions and data together
Support testing of software at granular level
Create a Class
Access Modifiers- private, public, protected
Inner Classes
Accessible inside parent class only
Storing data in instance variables
instance and static methods
constructor function
static variables as const
static final keywords
Enums
Inheritance
relationship between multiple classes
Java support single inheritance
classes can implement multiple interfaces
terms: base-derived, parent-child, superclass-subclass
Polymorphism
Interfaces
no implementation
can create an object of interface but need to write the methods
Abstract classes - combination of a class n interface
What's Java?
History
1991
goal: portability
write once, run everywhere
Unix, windows, mac, solaris etc
Oak
Oracle buys Sun
Principles & Components
Simple, Object Oriented, familier
Robust & secure
Portable
High performance
interpreted, threaded & dynamic
Runtime architecture
compiles to bytecode
bytecode is compatible with all OS
custom compile bytecode
core runtime and additional libs
JVM
OS
Java Editions
Standard
Core language & runtime environment
Enterprise Edition (EE)
Web Apps
Micro Edition
micro-controllers, sensors, & mobile devices
JRE
includes JVM
different OS versions
update to avoid security issues
not part of mobile OS, they have their own version of JRE
JDK
How Java Works?
Create .java Class
Show HelloWorld.java class example
use javac to compile to bytecode
use java to run bytecode
packages
reverse domain
Syntax
case sensitive
fullname, fullName, FULLNAME
semicolon to end statement
check naming rules
class name in uppercase, methods camelcase, const in uppercase
Memory Management & Garbage collection
Automatic
Stack & heap
stack is faster
Objects are stored in heap
primitives are stored in stack or heap
zero reference will be garbage collected
managed by java
garbage collector has its own thread
Development environments
Text Editor
Eclipse
Intellij Idea
Java Installation
Search "java jdk download"
http://www.oracle.com/technetwork/java/javase/downloads/index.html
Locate your OS version and download the exe
Set Environment variables
JAVA_HOME
path
Exception Handling and Debugging
Syntax error occur when we dont follow the Java grammer
It occurs compile time
Runtime error occurs when the logic is wrong
It occurs when the program is running
Runtime error can be handled thru try-catch block
Multiple catch blocks to handle multiple exceptions
Debugging through editors is quite easy
Use breakpoints, add to watch
Throwing your own exception and handling with catch block
Managing Program Flow
If-Then-Else blocks
Switch block
Looping
for, for-each, while, do-while
Function/Sub-routine/Method
Passing arguments as values or references
primitives are pass by value & objects are pass by reference
Data Collections
Arrays
int[] integers = {1, 2, 3}
Array.sort method
String strs = {"Sandip", "Priya", "Pihu"}
sized array: int[] sized = new int[10]
array copy: System.arrayCopy()
Multidimensional Arrays
ArrayList: resizable array
Map: to manage un-ordered data
Order of data is not guaranteed, it changes
Loop over collections using iterators
Iterators
for each loops
functional programming
Getting started
different parts of java class: public keyword, static void main method, arguments,
passing an argument, with space and without space example
package
reverse domain
folder hierarchy
compile with fully path of file com\example\java\HelloJava.java
run with fully qualified name com.example.java.HelloJava
Common Java Liberaries
File I/O
Apache file I/O
Reading file over internet
Deployment
JavaDocs
create docs folder in project main
use intellij generate javadocs from menu
Create a JAR file
Variables and Data
Datatypes
Whats Primitives
stores numbers, booleans, chars
stored in fastest available memory
Java is statically typed language, hence datatype has to be defined, not like javascript which is dynamically typed
int myVar = 5;
datatype identifier = value
compiler allocates memory based on the type
numeric primitive datatypes
datatype bits minimum maximum
byte 8 -128 127
short 16 -32768 32767
int 32
long 64
float 32
stores floating point numbers
double 64
stores floating point numbers
Equivalent helper classes
Byte, Short, Integer, Long, Float, Double
Helper class supports conversion and formatting
numeric primitives defaults to 0
primitives sysout example
primitives min max value example by using ++operator
there are issues with addition of primitive values like we have in JS
Use BigDecimal class to solve this issue
primitives are copies
widening and narrowing type
widening is automatic, no data loss
narrowing has chances of data loss and requires type casting
mathematical operations: +, -, *, /
operations with different types like double/int example
Math class
booleans
default value is false
local variables should be initialized
! operator
create using logical expression
chars
single character, string multiple chars
unicode example
Character class
Operators
Equality
==, !=, >, <, >=, <=, instanceof
Assignment
=
Mathematical
+, -,
, /, %, ++, --, +=, -=,
=, /=
postfix vs prefix increment
Logical
Objects