SlideShare a Scribd company logo
1 of 53
Variable in Java
Variable
Variable is name of reserved area allocated in memory. In
other words, it is a name of memory location. It is a
combination of "vary + able" that means its value can be
changed.
Java Basic
int data=10;//Here data is variable
Java Basic
Types of Variables:
There are three types of variables in java:
1) Local Variable
A variable declared inside the body of the method is called
local variable. You can use this variable only within that
method and the other methods in the class aren't even
aware that the variable exists.
A local variable cannot be defined with "static" keyword.
Java Basic
2) Instance Variable
A variable declared inside the class but outside the body
of the method, is called instance variable. It is not
declared as static.
It is called instance variable because its value is instance
specific and is not shared among instances.
3) Static variable
A variable which is declared as static is called static
variable. It cannot be local. You can create a single copy
of static variable and share among all the instances of the
class. Memory allocation for static variable happens only
once when the class is loaded in the memory.
Java example
Example to understand the types of variables in
java :
public class A
{
int data=50;//instance variable
static int m=100;//static variable
void method()
{
int n=90;//local variable
}
}//end of class
class Simple
{
public static void main(String[] args)
{
int a=10;
int b=10;
int c=a+b;
System.out.println(c);
}
}
Java example
class Simple{
public static void main(String[] args)
{
int a=10;
float f=a;
int b=(int)f;
System.out.println(a);
System.out.println(f);
}
}
Java example
class Simple
{
public static void main(String[] args){
float f=10.5f;
//int a=f;//Compile time error
int a=(int)f;
System.out.println(f);
System.out.println(a);
}
}
Java example
class Simple{
public static void main(String[] args){
//Overflow
int a=133;
byte b=(byte)a;
System.out.println(a);
System.out.println(b);
}}
output :
133 -123
Java example
class Simple
{
public static void main(String[] args){
byte a=10;
byte b=10;
//byte c=a+b;//Compile Time Error: because a+b=20 will be
int
byte c=(byte)(a+b);
System.out.println(c);
}
Java example
Data types specify the different sizes and values that can be
stored in the variable. There are two types of data types in
Java:
Primitive data types: The primitive data types include
Integer, Character, Boolean, and Floating Point.
Non-primitive data types: The non-primitive data types
include Classes, Interfaces, and Arrays.
Data Types in Java
Java Primitive Data Types :
In Java language, primitive data types are the building blocks
of data manipulation. These are the most basic data types
available in Java language.
Data Types in Java
There are 8 types of primitive data types:
boolean data type
byte data type
char data type
short data type
int data type
long data type
float data type
double data type
Data Types in Java
Data Types in Java
Data Types in Java
Data Type Default Value Default size
boolean false 1 bit
char 'u0000' 2 byte
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
float 0.0f 4 byte
double 0.0d 8 byte
Unicode System
Unicode is a universal international standard character
encoding that is capable of representing most of the world's
written languages.
Why java uses Unicode System ?
Before Unicode, there were many language standards:
 ASCII (American Standard Code for Information
Interchange) for the United States.
 ISO 8859-1 for Western European Language.
 KOI-8 for Russian.
 GB18030 and BIG-5 for chinese, and so on.
Unicode System
This caused two problems:
 A particular code value corresponds to different
letters in the various language standards.
 The encodings for languages with large character
sets have variable length.Some common characters
are encoded as single bytes, other require two or
more byte.
Unicode System
To solve these problems, a new language standard was
developed i.e. Unicode System.
In unicode, character holds 2 byte, so java also uses 2
byte for characters.
lowest value:u0000
highest value:uFFFF
Operators in Java
Operator in java is a symbol that is used to perform
operations. For example: +, -, *, / etc.
There are many types of operators in java which are
given below:
 Unary Operator
 Arithmetic Operator
 Shift Operator
 Relational Operator
 Bitwise Operator
 Logical Operator
 Ternary Operator
 Assignment Operator
Operators in Java
Operator Type Category Precedence
Unary postfix expr++, expr--
prefix ++expr ,--expr ,+expr, -
expr, ~ !
Arithmetic multiplicative *, / ,%
additive +, -
Shift shift <<, >>, >>>
Relational comparison < ,>, <=, >= ,instanceof
equality == , !=
Operators in Java
Operator Type Category Precedence
Bitwise bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
Logical logical AND &&
logical OR ||
Ternary ternary ? :
Assignment assignment = ,+=, -=, *= ,/= ,%=
,&=, ^=, |=, <<=, >>=,
>>>=
Java Unary Operator Example: ++ and --
class OperatorExample{
public static void main(String args[]){
int x=10;
System.out.println(x++);
System.out.println(++x);
System.out.println(x--);
System.out.println(--x);
}}
Java Unary Operator Example : ++ and --
class OperatorExample
{
public static void main(String args[]){
int a=10;
int b=10;
System.out.println(a++ + ++a);
System.out.println(b++ + b++);
}
}
Java Unary Operator Example: ~ and !
class OperatorExample{
public static void main(String args[]){
int a=10;
int b=-10;
boolean c=true;
boolean d=false;
System.out.println(~a);
System.out.println(~b);
System.out.println(!c);
System.out.println(!d);
}}
Java Unary Operator Example: ~ and !
System.out.println(~a)
-11 (minus of total positive value which starts from 0)
System.out.println(~b);
9 (positive of total minus, positive starts from 0)
System.out.println(!c);
false (opposite of boolean value)
System.out.println(!d);
true
Java Left Shift Operator Example
class OperatorExample{
public static void main(String args[]){
System.out.println(10<<2);
System.out.println(10<<3);
System.out.println(20<<2);
System.out.println(15<<4);
}}
Java Right Shift Operator Example
The operator ‘>>’ uses the sign bit (left most bit) to
fill the trailing positions after shift. If the number is
negative, then 1 is used as a filler and if the number is
positive, then 0 is used as a filler.
class OperatorExample
{
public static void main(String args[]){
System.out.println(10>>2);
System.out.println(20>>2);
System.out.println(20>>3);
}
}
Java Shift Operator Example: >> vs >>>
class OperatorExample{
public static void main(String args[]){
//For positive number, >> and >>> works same
System.out.println(20>>2);
System.out.println(20>>>2);
//For negative number, >>> changes parity bit
(MSB) to 0
System.out.println(-20>>2);
System.out.println(-20>>>2);
}}
output :
5 5 -5 1073741819
Logical && and Bitwise & Example
The logical && operator doesn't check second condition if
first condition is false. It checks second condition only if
first one is true.
The bitwise & operator always checks both conditions
whether first condition is true or false.
class OperatorExample{
public static void main(String args[]){
int a=10;
int b=5;
int c=20;
System.out.println(a<b&&a<c);//false && true = false
System.out.println(a<b&a<c);//false & true = false
}}
Logical && and Bitwise & Example
class OperatorExample{
public static void main(String args[]){
int a=10;
int b=5;
int c=20;
System.out.println(a<b&&a++<c);//false && true = false
System.out.println(a);
System.out.println(a<b&a++<c);//false & true = false
System.out.println(a);
}}
output :
false 10 false 11
Logical || and Bitwise | Example
The logical || operator doesn't check second condition if
first condition is true. It checks second condition only if
first one is false.
The bitwise | operator always checks both conditions
whether first condition is true or false.
Logical || and Bitwise | Example
class OperatorExample{
public static void main(String args[]){
int a=10;
int b=5;
int c=20;
System.out.println(a>b||a<c);//true || true = true
System.out.println(a>b|a<c);//true | true = true
//|| vs |
System.out.println(a>b||a++<c);//true || true = true
System.out.println(a);
System.out.println(a>b|a++<c);//true | true = true
System.out.println(a);
}}
Java Ternary Operator Example
Java Ternary operator is used as one liner replacement
for if-then-else statement and used a lot in java
programming. it is the only conditional operator which
takes three operands.
class OperatorExample{
public static void main(String args[]){
int a=2;
int b=5;
int min=(a<b)?a:b;
System.out.println(min);
}}
Operator Precedence
When operators have the same precedence, the earlier one
binds stronger
Selection Statements
•if-else statement
•if-else-if statement
Switch Statement
•switch provides a better alternative than if-else-if when the
execution follows several branches depending on the value
of an expression
Switch Statement
•Expression must be of type byte, short, int or char
•Each of the case values must be a literal of the compatible
type
•Case values must be unique
•Break makes sure that only the matching statement is
executed
Iteration Statement
Java iteration statements enable repeated execution of part
of a program until a certain termination condition becomes
true
•while statement
•How to run an infinte while loop ?
•do-while statement
Iteration Statement
for statement
•How to run an infinte for loop ?
Java Labeled For Loop
public class LabeledForExample {
public static void main(String[] args) {
aa:
for(int i=1;i<=3;i++){
bb:
for(int j=1;j<=3;j++){
if(i==2&&j==2){
break aa;
}
System.out.println(i+" "+j);
}
}
}
}
Java Labeled For Loop
Output:
1 1
1 2
1 3
2 1
Java Labeled For Loop
public class LabeledForExample2 {
public static void main(String[] args) {
aa:
for(int i=1;i<=3;i++){
bb:
for(int j=1;j<=3;j++){
if(i==2&&j==2){
break bb;
}
System.out.println(i+" "+j);
}
}
}
}
Java Labeled For Loop
Output:
1 1
1 2
1 3
2 1
3 1
3 2
3 3
Jump Statement
Java jump statements enable transfer of control to other
parts of program
•break statement
•break;
•Java does not have goto statement
•break label;
•label: { ... }
•continue statement
•The break statement terminates the block of code, in
particular it terminates the execution of an iterative
statement
Jump Statement
•The continue statement forces the early termination of the
current iteration to begin immediately the next iteration
•continue;
•continue label;
Java Break Statement with Loop
public class BreakExample {
public static void main(String[] args) {
for(int i=1;i<=10;i++){
if(i==5){
break;
}
System.out.println(i);
}
}
}
Java Break Statement with Loop
Output:
1
2
3
4
Java Break Statement with Inner Loop
public class BreakExample2 {
public static void main(String[] args) {
for(int i=1;i<=3;i++){
for(int j=1;j<=3;j++){
if(i==2&&j==2){
break;
}
System.out.println(i+" "+j);
}
}
}
}
Java Break Statement with Inner Loop
Output:
1 1
1 2
1 3
2 1
3 1
3 2
3 3
Java Continue Statement Example
public class ContinueExample {
public static void main(String[] args) {
for(int i=1;i<=10;i++){
if(i==5){
continue;
}
System.out.println(i);
}
}
}
Java Continue Statement Example
Output:
1
2
3
4
6
7
8
9
10
Java Continue Statement with Inner Loop
public class ContinueExample2 {
public static void main(String[] args) {
for(int i=1;i<=3;i++){
for(int j=1;j<=3;j++){
if(i==2&&j==2){
continue;
}
System.out.println(i+" "+j);
}
}
}
}
Java Continue Statement with Inner Loop
Output:
1 1
1 2
1 3
2 1
2 3
3 1
3 2
3 3

More Related Content

Similar to Basic_Java_02.pptx

Java PSkills-session2.pptx
Java PSkills-session2.pptxJava PSkills-session2.pptx
Java PSkills-session2.pptxssuser99ca78
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1Todor Kolev
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1Todor Kolev
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1Todor Kolev
 
DISE - Windows Based Application Development in Java
DISE - Windows Based Application Development in JavaDISE - Windows Based Application Development in Java
DISE - Windows Based Application Development in JavaRasan Samarasinghe
 
In this page, we will learn about the basics of OOPs. Object-Oriented Program...
In this page, we will learn about the basics of OOPs. Object-Oriented Program...In this page, we will learn about the basics of OOPs. Object-Oriented Program...
In this page, we will learn about the basics of OOPs. Object-Oriented Program...Indu32
 
Core Java introduction | Basics | free course
Core Java introduction | Basics | free course Core Java introduction | Basics | free course
Core Java introduction | Basics | free course Kernel Training
 
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arraysUnit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arraysDevaKumari Vijay
 
Core Java Programming | Data Type | operator | java Control Flow| Class 2
Core Java Programming | Data Type | operator | java Control Flow| Class 2Core Java Programming | Data Type | operator | java Control Flow| Class 2
Core Java Programming | Data Type | operator | java Control Flow| Class 2Sagar Verma
 
object oriented programming java lectures
object oriented programming java lecturesobject oriented programming java lectures
object oriented programming java lecturesMSohaib24
 
Lecture2_MCS4_Evening.pptx
Lecture2_MCS4_Evening.pptxLecture2_MCS4_Evening.pptx
Lecture2_MCS4_Evening.pptxSaqlainYaqub1
 
Programming with Java: the Basics
Programming with Java: the BasicsProgramming with Java: the Basics
Programming with Java: the BasicsJussi Pohjolainen
 
Introduction of Object Oriented Programming Language using Java. .pptx
Introduction of Object Oriented Programming Language using Java. .pptxIntroduction of Object Oriented Programming Language using Java. .pptx
Introduction of Object Oriented Programming Language using Java. .pptxPoonam60376
 

Similar to Basic_Java_02.pptx (20)

Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
 
Java PSkills-session2.pptx
Java PSkills-session2.pptxJava PSkills-session2.pptx
Java PSkills-session2.pptx
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
 
Java chapter 3
Java chapter 3Java chapter 3
Java chapter 3
 
DISE - Windows Based Application Development in Java
DISE - Windows Based Application Development in JavaDISE - Windows Based Application Development in Java
DISE - Windows Based Application Development in Java
 
In this page, we will learn about the basics of OOPs. Object-Oriented Program...
In this page, we will learn about the basics of OOPs. Object-Oriented Program...In this page, we will learn about the basics of OOPs. Object-Oriented Program...
In this page, we will learn about the basics of OOPs. Object-Oriented Program...
 
DITEC - Programming with Java
DITEC - Programming with JavaDITEC - Programming with Java
DITEC - Programming with Java
 
Core Java introduction | Basics | free course
Core Java introduction | Basics | free course Core Java introduction | Basics | free course
Core Java introduction | Basics | free course
 
Java basic concept
Java basic conceptJava basic concept
Java basic concept
 
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arraysUnit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
 
Core Java Programming | Data Type | operator | java Control Flow| Class 2
Core Java Programming | Data Type | operator | java Control Flow| Class 2Core Java Programming | Data Type | operator | java Control Flow| Class 2
Core Java Programming | Data Type | operator | java Control Flow| Class 2
 
object oriented programming java lectures
object oriented programming java lecturesobject oriented programming java lectures
object oriented programming java lectures
 
Lecture2_MCS4_Evening.pptx
Lecture2_MCS4_Evening.pptxLecture2_MCS4_Evening.pptx
Lecture2_MCS4_Evening.pptx
 
Introduction to Java
Introduction to JavaIntroduction to Java
Introduction to Java
 
Operators
OperatorsOperators
Operators
 
Java session4
Java session4Java session4
Java session4
 
Programming with Java: the Basics
Programming with Java: the BasicsProgramming with Java: the Basics
Programming with Java: the Basics
 
Introduction of Object Oriented Programming Language using Java. .pptx
Introduction of Object Oriented Programming Language using Java. .pptxIntroduction of Object Oriented Programming Language using Java. .pptx
Introduction of Object Oriented Programming Language using Java. .pptx
 

Recently uploaded

My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Hyundai Motor Group
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 

Recently uploaded (20)

My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 

Basic_Java_02.pptx

  • 1. Variable in Java Variable Variable is name of reserved area allocated in memory. In other words, it is a name of memory location. It is a combination of "vary + able" that means its value can be changed.
  • 2. Java Basic int data=10;//Here data is variable
  • 3. Java Basic Types of Variables: There are three types of variables in java: 1) Local Variable A variable declared inside the body of the method is called local variable. You can use this variable only within that method and the other methods in the class aren't even aware that the variable exists. A local variable cannot be defined with "static" keyword.
  • 4. Java Basic 2) Instance Variable A variable declared inside the class but outside the body of the method, is called instance variable. It is not declared as static. It is called instance variable because its value is instance specific and is not shared among instances. 3) Static variable A variable which is declared as static is called static variable. It cannot be local. You can create a single copy of static variable and share among all the instances of the class. Memory allocation for static variable happens only once when the class is loaded in the memory.
  • 5. Java example Example to understand the types of variables in java : public class A { int data=50;//instance variable static int m=100;//static variable void method() { int n=90;//local variable } }//end of class
  • 6. class Simple { public static void main(String[] args) { int a=10; int b=10; int c=a+b; System.out.println(c); } } Java example
  • 7. class Simple{ public static void main(String[] args) { int a=10; float f=a; int b=(int)f; System.out.println(a); System.out.println(f); } } Java example
  • 8. class Simple { public static void main(String[] args){ float f=10.5f; //int a=f;//Compile time error int a=(int)f; System.out.println(f); System.out.println(a); } } Java example
  • 9. class Simple{ public static void main(String[] args){ //Overflow int a=133; byte b=(byte)a; System.out.println(a); System.out.println(b); }} output : 133 -123 Java example
  • 10. class Simple { public static void main(String[] args){ byte a=10; byte b=10; //byte c=a+b;//Compile Time Error: because a+b=20 will be int byte c=(byte)(a+b); System.out.println(c); } Java example
  • 11. Data types specify the different sizes and values that can be stored in the variable. There are two types of data types in Java: Primitive data types: The primitive data types include Integer, Character, Boolean, and Floating Point. Non-primitive data types: The non-primitive data types include Classes, Interfaces, and Arrays. Data Types in Java
  • 12. Java Primitive Data Types : In Java language, primitive data types are the building blocks of data manipulation. These are the most basic data types available in Java language. Data Types in Java
  • 13. There are 8 types of primitive data types: boolean data type byte data type char data type short data type int data type long data type float data type double data type Data Types in Java
  • 15. Data Types in Java Data Type Default Value Default size boolean false 1 bit char 'u0000' 2 byte byte 0 1 byte short 0 2 byte int 0 4 byte long 0L 8 byte float 0.0f 4 byte double 0.0d 8 byte
  • 16. Unicode System Unicode is a universal international standard character encoding that is capable of representing most of the world's written languages. Why java uses Unicode System ? Before Unicode, there were many language standards:  ASCII (American Standard Code for Information Interchange) for the United States.  ISO 8859-1 for Western European Language.  KOI-8 for Russian.  GB18030 and BIG-5 for chinese, and so on.
  • 17. Unicode System This caused two problems:  A particular code value corresponds to different letters in the various language standards.  The encodings for languages with large character sets have variable length.Some common characters are encoded as single bytes, other require two or more byte.
  • 18. Unicode System To solve these problems, a new language standard was developed i.e. Unicode System. In unicode, character holds 2 byte, so java also uses 2 byte for characters. lowest value:u0000 highest value:uFFFF
  • 19. Operators in Java Operator in java is a symbol that is used to perform operations. For example: +, -, *, / etc. There are many types of operators in java which are given below:  Unary Operator  Arithmetic Operator  Shift Operator  Relational Operator  Bitwise Operator  Logical Operator  Ternary Operator  Assignment Operator
  • 20. Operators in Java Operator Type Category Precedence Unary postfix expr++, expr-- prefix ++expr ,--expr ,+expr, - expr, ~ ! Arithmetic multiplicative *, / ,% additive +, - Shift shift <<, >>, >>> Relational comparison < ,>, <=, >= ,instanceof equality == , !=
  • 21. Operators in Java Operator Type Category Precedence Bitwise bitwise AND & bitwise exclusive OR ^ bitwise inclusive OR | Logical logical AND && logical OR || Ternary ternary ? : Assignment assignment = ,+=, -=, *= ,/= ,%= ,&=, ^=, |=, <<=, >>=, >>>=
  • 22. Java Unary Operator Example: ++ and -- class OperatorExample{ public static void main(String args[]){ int x=10; System.out.println(x++); System.out.println(++x); System.out.println(x--); System.out.println(--x); }}
  • 23. Java Unary Operator Example : ++ and -- class OperatorExample { public static void main(String args[]){ int a=10; int b=10; System.out.println(a++ + ++a); System.out.println(b++ + b++); } }
  • 24. Java Unary Operator Example: ~ and ! class OperatorExample{ public static void main(String args[]){ int a=10; int b=-10; boolean c=true; boolean d=false; System.out.println(~a); System.out.println(~b); System.out.println(!c); System.out.println(!d); }}
  • 25. Java Unary Operator Example: ~ and ! System.out.println(~a) -11 (minus of total positive value which starts from 0) System.out.println(~b); 9 (positive of total minus, positive starts from 0) System.out.println(!c); false (opposite of boolean value) System.out.println(!d); true
  • 26. Java Left Shift Operator Example class OperatorExample{ public static void main(String args[]){ System.out.println(10<<2); System.out.println(10<<3); System.out.println(20<<2); System.out.println(15<<4); }}
  • 27. Java Right Shift Operator Example The operator ‘>>’ uses the sign bit (left most bit) to fill the trailing positions after shift. If the number is negative, then 1 is used as a filler and if the number is positive, then 0 is used as a filler. class OperatorExample { public static void main(String args[]){ System.out.println(10>>2); System.out.println(20>>2); System.out.println(20>>3); } }
  • 28. Java Shift Operator Example: >> vs >>> class OperatorExample{ public static void main(String args[]){ //For positive number, >> and >>> works same System.out.println(20>>2); System.out.println(20>>>2); //For negative number, >>> changes parity bit (MSB) to 0 System.out.println(-20>>2); System.out.println(-20>>>2); }} output : 5 5 -5 1073741819
  • 29. Logical && and Bitwise & Example The logical && operator doesn't check second condition if first condition is false. It checks second condition only if first one is true. The bitwise & operator always checks both conditions whether first condition is true or false. class OperatorExample{ public static void main(String args[]){ int a=10; int b=5; int c=20; System.out.println(a<b&&a<c);//false && true = false System.out.println(a<b&a<c);//false & true = false }}
  • 30. Logical && and Bitwise & Example class OperatorExample{ public static void main(String args[]){ int a=10; int b=5; int c=20; System.out.println(a<b&&a++<c);//false && true = false System.out.println(a); System.out.println(a<b&a++<c);//false & true = false System.out.println(a); }} output : false 10 false 11
  • 31. Logical || and Bitwise | Example The logical || operator doesn't check second condition if first condition is true. It checks second condition only if first one is false. The bitwise | operator always checks both conditions whether first condition is true or false.
  • 32. Logical || and Bitwise | Example class OperatorExample{ public static void main(String args[]){ int a=10; int b=5; int c=20; System.out.println(a>b||a<c);//true || true = true System.out.println(a>b|a<c);//true | true = true //|| vs | System.out.println(a>b||a++<c);//true || true = true System.out.println(a); System.out.println(a>b|a++<c);//true | true = true System.out.println(a); }}
  • 33. Java Ternary Operator Example Java Ternary operator is used as one liner replacement for if-then-else statement and used a lot in java programming. it is the only conditional operator which takes three operands. class OperatorExample{ public static void main(String args[]){ int a=2; int b=5; int min=(a<b)?a:b; System.out.println(min); }}
  • 34. Operator Precedence When operators have the same precedence, the earlier one binds stronger
  • 36. Switch Statement •switch provides a better alternative than if-else-if when the execution follows several branches depending on the value of an expression
  • 37. Switch Statement •Expression must be of type byte, short, int or char •Each of the case values must be a literal of the compatible type •Case values must be unique •Break makes sure that only the matching statement is executed
  • 38. Iteration Statement Java iteration statements enable repeated execution of part of a program until a certain termination condition becomes true •while statement •How to run an infinte while loop ? •do-while statement
  • 39. Iteration Statement for statement •How to run an infinte for loop ?
  • 40. Java Labeled For Loop public class LabeledForExample { public static void main(String[] args) { aa: for(int i=1;i<=3;i++){ bb: for(int j=1;j<=3;j++){ if(i==2&&j==2){ break aa; } System.out.println(i+" "+j); } } } }
  • 41. Java Labeled For Loop Output: 1 1 1 2 1 3 2 1
  • 42. Java Labeled For Loop public class LabeledForExample2 { public static void main(String[] args) { aa: for(int i=1;i<=3;i++){ bb: for(int j=1;j<=3;j++){ if(i==2&&j==2){ break bb; } System.out.println(i+" "+j); } } } }
  • 43. Java Labeled For Loop Output: 1 1 1 2 1 3 2 1 3 1 3 2 3 3
  • 44. Jump Statement Java jump statements enable transfer of control to other parts of program •break statement •break; •Java does not have goto statement •break label; •label: { ... } •continue statement •The break statement terminates the block of code, in particular it terminates the execution of an iterative statement
  • 45. Jump Statement •The continue statement forces the early termination of the current iteration to begin immediately the next iteration •continue; •continue label;
  • 46. Java Break Statement with Loop public class BreakExample { public static void main(String[] args) { for(int i=1;i<=10;i++){ if(i==5){ break; } System.out.println(i); } } }
  • 47. Java Break Statement with Loop Output: 1 2 3 4
  • 48. Java Break Statement with Inner Loop public class BreakExample2 { public static void main(String[] args) { for(int i=1;i<=3;i++){ for(int j=1;j<=3;j++){ if(i==2&&j==2){ break; } System.out.println(i+" "+j); } } } }
  • 49. Java Break Statement with Inner Loop Output: 1 1 1 2 1 3 2 1 3 1 3 2 3 3
  • 50. Java Continue Statement Example public class ContinueExample { public static void main(String[] args) { for(int i=1;i<=10;i++){ if(i==5){ continue; } System.out.println(i); } } }
  • 51. Java Continue Statement Example Output: 1 2 3 4 6 7 8 9 10
  • 52. Java Continue Statement with Inner Loop public class ContinueExample2 { public static void main(String[] args) { for(int i=1;i<=3;i++){ for(int j=1;j<=3;j++){ if(i==2&&j==2){ continue; } System.out.println(i+" "+j); } } } }
  • 53. Java Continue Statement with Inner Loop Output: 1 1 1 2 1 3 2 1 2 3 3 1 3 2 3 3