Java is an object-oriented programming language and platform. It uses a virtual machine (JVM) to run bytecode, making Java programs platform independent. The JVM handles memory management through garbage collection. Java uses classes and objects to implement OOP concepts. Variables are declared with a specific primitive data type or class type. Java supports control flow statements like if/else and loops. Arrays allow storing multiple values of the same type. Methods define reusable blocks of code that can operate on objects.
2. What is Java ?
• Java
- Java is not just a programming language but it is a complete
platform for object oriented programming.
• JRE
- Java standard class libraries which provide Application
Programming Interface and JVM together form JRE (Java Runtime
Environment).
• JDK
- JDK (Java development kit) provides all the needed support for
software development in Java.
3. Java Virtual Machine (JVM)
• Runs the Byte Code.
• Makes Java platform independent.
• Handles Memory Management.
4. Garbage Collection
• The programmer is not responsible for memory
management in Java.
• The memory area in JVM where objects are created
is called Heap.
• The memory is freed during runtime by a special
thread called Garbage Collector.
• The GC looks for objects which are no longer
needed by the program and destroys them.
6. How Java works ?
• Java compilers convert your code from human readable to
something called “bytecode” in the Java world.
• “Bytecode” is interpreted by a JVM, which operates much
like a physical CPU to actually execute the compiled code.
• Just-in-time (JIT) compiler is a program that turns
Java bytecode into instructions that can be sent directly to
the processor.
9. Data Types in Java
• Data types define the nature of a value
• We need different data-types to handle real-world information
Name Size (in bits) Range
long 64 -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
int 32 –2,147,483,648 to 2,147,483,647
Short 16 –32,768 to 32,767
byte 8 –128 to 127
double 64 4.9e–324 to 1.8e+308
float 32 1.4e–045 to 3.4e+038
char 16 0 to 65,536
boolean ??
true/false
10. Primitive Types and Variables
• boolean, char, byte, short, int, long, float, double etc.
• These basic (or primitive) types are the only types that are
not objects (due to performance issues).
• This means that you don’t use the new operator to create
a primitive variable.
11. Naming Convention of Variables
• Can start with a letter, an underscore(_), or a dollar sign ($)
• Cannot start with a number.
long _LongNumber = 9999999;
String firstName = “John”;
float $Val = 2.3f;
int i, index = 2;
double gamma = 1.2;
boolean value2 = false;
12. Operators
• Provide a way to perform different operations on
variables
• Categories of Java Operators
Assignment Operators =
Arithmetic Operators - + * / %
Relational Operators > < >= <= == !=
Logical Operators && || & | ^
Unary Operators + - ++ -- !
13. Assignment and Arithmetic Operators
• Used to assign a value to a variable
• Syntax
– <variable> = <expression>
Assignment Operator =
• Java provides eight Arithmetic operators:
– for addition, subtraction, multiplication, division, modulo (or
remainder), increment (or add 1), decrement (or subtract
1), and negation.
14. Relational Operators
• Used to compare two values.
• Binary operators, and their operands are numeric
expressions.
Relational Operators > < >= <= == !=
15. Logical Operators
• Return a true or false value based on the state of
the variables
• There are six logical operators
Conditional AND Conditional OR AND OR NOT Exclusive OR
Logical Operators && || & | ! ^
16. Static versus Non-static Variables
• Static variables are shared across all the objects of a
class
– There is only one copy
• Non-Static variables are not shared
– There is a separate copy for each individual live object.
• Static variables cannot be declared within a
method.
17. Statements & Blocks
• A simple statement is a command terminated by a semi-colon:
name = “Fred”;
• A block is a compound statement enclosed in curly brackets:
{
name1 = “Fred”; name2 = “Bill”;
}
• Blocks may contain other blocks.
18. Flow of Control
• Java executes one statement after the other in the
order they are written.
• Many Java statements are flow control statements:
Alternation:
if, if else, switch
Looping:
for, while, do while
19. Java Basic Constructs
• If…else
• Switch Statement
• For Loop
• While Loop
• Do...While Loop
20. If else– Syntax
if ( <condition> )
{
// Execute these statements if <condition> is TRUE
}
else
{
// Execute these statements if < condition> is FALSE
}
23. While – Syntax
while (condition)
{
statement 1;
statement 2; . .
}
Sample:
int i=0;
while (i<5)
{
System.out.println(i);
i++;
}
24. Do While – Syntax
do
{
statement 1;
statement 2; . .
} while (condition) ;
Sample:
int i=0;
do
{
System.out.println(i);
i++;
} while (i<5);
25. Arrays
• An array is a list of similar things.
• An array has a fixed:
– name
– type
– length
• These must be declared when the array is created.
• Array size cannot be changed during the execution of the
code.
26. Example of an Array
myArray = 3 6 3 1 6 3 4 1
0 1 2 3 4 5 6 7
myArray has room for 8 elements
the elements are accessed by their index.
in Java, array indices starts at 0.
27. Some more Examples
float[] anArrayOfFloats = new float[5];
Car [] cars = new Car()[3]; // an array of cars capable
of storing 3 car objects
Object[] anArrayOfObjects = new Objects[100];
boolean[] answers = { true, false, true, true, false };
How to get the length of an array object ?
cars.length ✔ way
cars.length() ✗ way
29. Java OOP
• Create new object type with class keyword.
• A class definition can contain:
– variables (fields)
– initialization code
– methods
Java Programming: OOP 29
30. An Example class
package com.edureka.entity; // package
public class Car{ //class declaration
String name;
String color;
float weight;
...
public void move(){ // method
...
}
}
31. Objects
• Runtime representation of a class.
• Objects hold state with variables
• Objects do some work with methods.
• Can be created during Runtime as follows :
Car carRef = new Car();
carRef New Car()
Object Reference Newly created Object
32. Methods
• A method is a named sequence of code that can be invoked
by other Java code.
• A method takes some parameters, performs some
computations and then optionally returns a value (or object).
• Methods can be used as part of an expression statement.
Ex:
public float convert_to _Celsius( float temp) {
return(((temp * 9.0f) / 5.0f) + 32.0f );
}
33. Modifiers
• public: any method (in any class) can access the
field.
• protected: any method in the same package or any
derived class can access the field.
• private: only methods within the class can access the
field.
• default is that only methods in the same package
can access the field.
Java Programming: OOP 33
Not just a programming language but a complete platformThe Programming language Java standard class libraries which provide Application Programming Interface and JVM together form JRE (Java Runtime Environment)JDK (Java development kit) provides all the needed support for software development in Java.
Need a diagram to show garbage collection
Java compilers convert your code from human readable Java source files to something called “bytecode” in the Java world. “Bytecode” is interpreted by a JVM, which operates much like a physical CPU might operate on machine code, to actually execute the compiled code. Performance - Java performance in generally second only to C/C++ in common language performance comparisons. In the Java programming language and environment, a just-in-time (JIT) compiler is a program that turns Java bytecode (a program that contains instructions that must be interpreted) into instructions that can be sent directly to the processor.The just-in-time compiler comes with the virtual machine and is used optionally. It compiles the bytecode into platform-specific executable code that is immediately executed
Java compilers convert your code from human readable Java source files to something called “bytecode” in the Java world. “Bytecode” is interpreted by a JVM, which operates much like a physical CPU might operate on machine code, to actually execute the compiled code. Performance - Java performance in generally second only to C/C++ in common language performance comparisons. In the Java programming language and environment, a just-in-time (JIT) compiler is a program that turns Java bytecode (a program that contains instructions that must be interpreted) into instructions that can be sent directly to the processor.The just-in-time compiler comes with the virtual machine and is used optionally. It compiles the bytecode into platform-specific executable code that is immediately executed
Adv. Question : What is the range of primitive data types ?Initalization:If no value is assigned prior to use, then the compiler will give an errorJava sets primitive variables to zero or false in the case of a boolean variableAll object references are initially set to null
Java provides six relational operators:greater than (>), less than (<), greater than or equal (>=), less than or equal (<=), equal (==), and not equal (!=).
Is it really needed to define all the types of operators.It will be better to show their differences through a program.
DemoBroke into multiple sentences
DemoReformatted and used clip art for right and wrong
Demo
Public/PrivateMethods/data may be declared public or private meaning they may or may not be accessed by code in other classes …Good practice:keep data privatekeep most methods privatewell-defined interface between classes - helps to eliminate errors