JAVA BACKEND
DEVELOPMENT
PROGRAM
Java SE Basics
OUTLINE
• JVM
• Primitive and Non-primitive Data Types
• Comment
• Operator
• Flow control
• Keywords
JVM (Java Virtual Machine)
● A virtual machine that provides a runtime
environment for executing Java
bytecode
● Allow Java applications to run on any
device with a JVM installed ("Write once,
run anywhere" principle)
JVM (Java Virtual Machine)
● Core components include:
○Class Loader
loads and links classes
■
○Runtime Data Areas
memory management
■
○Execution Engine
interprets and executes bytecode
■
Java Code Execution
VARIABLE
• A variable is a named memory location
capable of storing data
• Object variables refer to objects, which are
created by instantiating classes with the new
keyword.
For example: List<Integer> list = new ArrayList<>();
• We can also store data in simple variables,
which represent data only, without any
associated methods, i.e. primitives.
• Declaration
VARIABLE
• Data Type
• Name
• int noOfWatts;
• Initialization
• Name
• Value
• Assignment operator
• noOfWatts = 100;
• Combined:
• int noOfWatts = 100;
VARIABLE
• Data types
• Primitive data types: built-in types
• Non primitive data types: objects,
which contain fields and methods
VARIABLE
• Primitive Data Types
•
CONVERSION AND
CASTING
• Conversion, also known as widening conversion, are performed automatically
• A smaller box can be placed in a bigger box and so on.
• Casting also known as narrowing conversion.
• A bigger box has to be placed in a smaller box.
• Casting is not implicit in nature.
• int i = (int)(8.0/3.0);
• Casting will lose precision
•
CONVERSION AND
CASTING
WRAPPER CLASS
• A Wrapper class is a class whose object wraps or
contains a primitive data type. When we create an
object from a wrapper class, it contains a field,
and in this field, we can store a primitive data type.
• Byte, Short, Integer, Short, Long, Double,
Character, Boolean
STRING
• A String is a sequence of characters. In java,
String objects are immutable, which means
that once they are created, they cannot be
changed.
• String Pool — a collection of Strings which
are stored in the heap memory
Memory Layout in Java
STACK MEMORY
• Stack : used for static memory allocation
and the execution of a thread
• Contains primitive values
• References to objects that are in the heap
HEAP SPACE
• Heap : dynamic memory allocation for
Objects
• Objects can be globally accessed.
STACK MEMORY
HEAP SPACE
• String Pool: Save memory,
reusability(don’t need to create a new
String if one already exists)
COMMENT
• Java supports three types of comments
• 1. /* text */
• The compiler ignores everything from /* to */.
• 2. //text
• The compiler ignores everything from // to the end of the line.
• 3. /** text */
• This is a documentation comment and in general its called doc
comment. The JDK javadoc tool uses doc comments when preparing
automatically generated documentation.
OPERATOR
• Arithmetic operations in Java
• Precedence: (*, /, %) > (+, -)
• Parentheses (): evaluate the innermost
parenthesized expression first, and work
your way out through the levels of nesting
• No {} or [ ] in terms of arithmetic operations
in Java
OPERATOR
Examples:
int x = 5, y = 2, z;
z = x + y * 2; //9
z = (x + y) * 2; //14
z = x / y; //2
COMPOUND ARITHMETIC/
ASSIGNMENT OPERATORS
Operator Example Meaning
+= X += 1; X = X + 1;
-= X -= 1; X = X – 1;
*= X *= 5; X = X * 5;
/= X /= 2; X = X / 2;
%= X %= 10; X = X % 10;
INCREMENT AND
DECREMENT
OPERATORS
JAVA LOGICAL OPERATOR
Operator Name Type Description
Returns true if the operand to the right evaluates to false.
! Not Unary Returns false if the operand to the right is true.
If the operand on the left returns false, it returns false
&& Conditional And Binary without evaluating the operand on the right.
If the operand on the left returns true, it returns true without
|| Conditional Or Binary evaluating the operand on the right.
RELATIONAL OPERATORS
Operator Result
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
FLOW CONTROL
• Selection Statements
• If and switch
• Iteration Statements
• while, do-while, for and nested
loops
• Jump Statements
• Break, continue and return
SWITCH
A
B A
Defaul
t
IF
TERNARY EXPRESSION
• Variable = (condition) ? expressionTrue : expressionFalse
•
ITERATION
STATEMENT
• While
• Do-While
• For
WHILE
DO WHILE
FO
R
JUMP STATEMENT
• Break
• Continue
• Return
Break
Continue
JUMP STATEMENT
KEYWORDS
• Class
• Modifier
• Static
CLASS
• A class is a container that contains the block
of code that includes fields, methods,
constructors, getters and setters.
•
MODIFIER
• Access Modifier
• Specify accessibility of field, method,
constructor & class
• Non-Access Modifier
• e.g. static, abstract, final
ACCESS MODIFIER
• Determine access rights for the class and its
members
• public
• private
• protected
• default
ACCESS MODIFIER SCOPE
NON-ACCESS MODIFIER
• Static
• Final
• Abstract
STATIC
• Static:
• Class
• compile time or early binding
• Non-Static
• Object
• Runtime time or dynamic binding
CLASS VARIABLE
INSTANCE VARIABLE
• What is the difference between
following statements?
• public int x;
• public static int x;
CLASS VARIABLE
INSTANCE VARIABLE
CLASS VARIABLE
INSTANCE VARIABLE
STATIC METHOD
NON-STATIC
•
METHOD
What is the difference between
following statements?
• public int getX();
• public static int getX();
STATIC METHOD
NON-STATIC
METHOD
MAIN METHOD
• main is a method
• public — main can be called from outside the class
• Static — main can be called by the JVM without instantiating
an object
• Void — main does not return a value
Final
• The final keyword can be used to make a class, method or
variable immutable.
• Final variable:
• Once a final variable is assigned a value, it becomes a
constant and can no longer be changed.
• Final method:
• Once a method is made final, it cannot be overridden.
• Final Class:
• Once a class is made final, it cannot be extended.
DEEP COPY VS SHALLOW COPY
• In OOP languages like Java, object copying is creating an exact copy of
an existing object.
• Shallow copy:
• If you modify the copied object, the original object will be modified
as well. The shallow copy points to the reference of the original
object.
• To do shallow copy, we use the default clone() method.
• Deep copy:
• If you modify the copied object, the original object will not be
modified. The copy has its own reference address.
• To do a deep copy, we use the new keyword and copy over the
values one by one.
DEEP COPY VS SHALLOW COPY
• Shallow copy Deep copy
QUESTION
S?