Java Essentials for Hadoop
Agenda What is Java ? Features of Java How Java Works Java Data Types Java Operators Statements & Blocks in Java Java Class Java Basic Constructs Creating an Object Arrays
What is Java ? Java is a high level programming language developed by Sun Microsystems and released in November 1995. Java can be used to create complete applications that may run on a single computer or be distributed among servers and clients in a network.
Java is Object-Oriented and follows the principal of “write once run anywhere� codes.
Features of Java Simple Object Oriented Multithreaded Distributed
Platform Independent Secure Robust
What is JDK and JRE ?
JDK JRE
+ JVM
Utility Classes
Development tools
Compiler Debugger
How Java Works ? It is used to speed up the execution Hexadecimal format
JIT Compiler
Source Code
Java Compiler
Byte Code
Java Interpreter JVM Converts total byte code into machine code
Runtime System
Output of the program
Data Types Primitive Data Types Data Type
Size
Default value
Boolean
1 bit
False
char
2 bytes
'\u0000'
byte
1 byte
0
short
2 bytes
0
int
4 bytes
0
long
8 bytes
0L
float
4 bytes
0.0f
double
8 bytes
0.0d
String(any object)
-
null
Non-primitive Data Types (Reference type)
1. 2. 3.
Class Type Interface Type Array type
Operators Provide a way to perform different operations on variables. Operator Type Assignment Arithmetic Relational Logical Bitwise Unary
operators = +,- , *, / < , > , <= , >= , == , != && , || , ! &, |, ^, >>, << ++ , --, +, -, !
Statements & Blocks in JAVA Statement forms a complete command to be executed. Statements are always terminated by a semi-colon(;) class Stat { Boolean flag ; //statement 1 Int x = 20 ; //Statement 2 . . }
A block is a compound statement enclosed in curly brackets.
Open braces for a block
End of block, using close braces
Java Class A class can be defined as a template/blue print that describes the behaviors and states that object of its type support. Objects have states and behaviors. Example: Bicycles have state (current gear, current pedal cadence, two wheels, number of gears) and behavior (braking, accelerating, slowing down, changing gears).
An object is an instance of a class.
Example of a class class Myclass { //data members //constructors // methods or member functions }
public class hello { public static void main(String[] args) { int number = 10; //variable System.out.println("Hello"); System.out.println(number); } }
Method Java keyword
Modifier
function name
return type
Any number of arguments
Public static void myFunction(arg1, arg2, arg3) {
//Body of the method }
Modifiers Public
Protected
Default
Private
• Can be accessed from anywhere. • Can be accessed in the same package or any derived class. • Can be accessed within the same package.
• Can be accessed within the class only.
Java Basic Constructs If…else case
Switch Case Loops in Java – for loop, while loop, do while loop
If Else - Syntax
true condition
false
Statements
If (condition) { Statements } else { Statements }
Switch - Syntax Case 1
true
Switch(x) {
Statements
Case 1: Statements
false
Case 2
true
Statements
Case 2: Statements
false
default : Statements }
default End of the switch Statement
For Loop - Syntax
Initialization
Increment/ decrement
condition
false End of for loop
true
Inside the for loop
for (initialization; condition; increment/decrement;) { statement 1; statement 2; â&#x20AC;Ś }
While Loop - Syntax Initialization Increment/ decrement
condition
false
End of for loop
true
Inside the for loop
while (<condition>) { statement1; â&#x20AC;Ś }
Do While Loop Syntax Initialization
condition
do { statement 1; statement 2; â&#x20AC;Śâ&#x20AC;Ś
Inside the do while loop
Increment/ decrement
} while (<condition>) ; true condition
false
End of for loop
Creation of the Object
Name of the class
Allocate memory
Creates a new object
Class_name object_name = new class_name(); Creates a reference object
Constructor of the class
Arrays An array is a list of similar elements. 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.