SlideShare ist ein Scribd-Unternehmen logo
1 von 50
www.SunilOS.com 1
Variables & Operators
www.sunilos.com
www.raystec.com
Variables
 Variable stores human data
like numbers and alphabets.
 Data type will decide what
values will be stored in
variables.
 You can say data type will
define the structure of your
data.
www.SunilOS.com 2
Variables and Data Types
Decimal values will be stored in float and
double data type.
Non-decimals values will be stored in int,
long, byte, and short data types.
Character will be stored in char data type.
True/False will be stored in boolean data type.
www.SunilOS.com 3
www.SunilOS.com 4
Data Types
Data types are divided into two categories.
Primitive Data Types
o byte, short, int, long, float, double,
boolean, char.
o It occupies number of bytes as per data type.
o It stores values.
Reference Data Types
o It stores memory address of a value.
o It occupies 2 bytes to store a reference (memory
address).
o Strings, Objects, Arrays are reference data types.
www.SunilOS.com 5
Primitive Data Types
int
long
byte
short
float
double
1
2
4
8
4
8
-128, +127
-9.223E18, +9.223E18
-32768, +32767
-2147483648, +2147483647
+3.4 E+38
+1.7 E+308
Type Size Byte Range
char 2 0, 65535
boolean 1 true, false
0
0
0
0
0
0
0
false
Default
www.SunilOS.com 6
Declare Variable
int total;
total = 5+6;
short srt;
srt = 3;
char ch;
ch =‘A’ ;
4 Bytes
Stack Memory1001
total 1001
11
2 Bytes
1011
3
srt 1011
2 Bytes
1010
A
ch 1010
www.SunilOS.com 7
Declare Object
 int total;
 total = 5+6;
 String str;
 str = “sunRays”;
o or
 str = new
String(“sunRays”);
4 Bytes
Stack Memory
1001
total 1001
11
2 Bytes
1011
1010
str 1011
14 Bytes
1010
SUNRAYS
www.SunilOS.com 8
Declare Variable – Primitive Data
 int total;
 total = 5+6;
 int newTotal;
 newTotal = total ;
 newTotal =
newTotal+3 ;
4 Bytes
Stack Memory1001
total 1001
11
4 Bytes
1011
newTotal 1011
1114
www.SunilOS.com 9
Declare Object - Copy reference
 String str;
 str = “sunRays”
 Or
 str = new
String(“sunRays”)
 String newStr;
 newStr = str;
Stack Memory
2 Bytes
1011
1010
str 1011
14 Bytes
1010
SUNRAYS
2 Bytes
1110
1010
newStr 1110
Java Identifier
It is a name of:
o Variable
o Method
o Class
o Interface
o Package
Used to identify a variable, method and
class in its scope.
www.SunilOS.com 10
www.SunilOS.com 11
Java Identifier Rules
 Name of an Identifier follows certain rules. Here are key
rules:
o The first character must be a non-digit character from the Unicode
standard String firstName;
o Subsequent characters may include digits int total123 ;
o Java is case sensitive that means Character Case is significant
• int count =0 ; Count = 1 ; are two different variables
o Avoid using underscore (_) and $ for the first character.
o User-defined identifiers can not duplicate Java keywords.
www.SunilOS.com 12
What’s an operator?
Operators are tokens that trigger some
computation when applied to variables and
other objects.
It can be categorized into:
o Arithmetic
o logical
o bit-level and
o Class access operators.
www.SunilOS.com 13
Java operators
()
++
--
!
~
instance of
*
/
%
+
-
<<
>>
>>>
<
>
<=
>=
==
!=
&
^
|
&&
||
?:
=
op=
www.SunilOS.com 14
Operator Precedence
int a = 2+ 4 + 8;
int a = 2+ 4 * 8;
int a = b = c = 5;
www.SunilOS.com 15
Operator Precedence
Operators Precedence
postfix expr++ expr--
unary ++expr --expr +expr -expr ~ !
multiplicative * / %
additive + -
shift << >> >>>
relational < > <= >= instanceof
equality == !=
bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
logical AND &&
logical OR ||
conditional ? :
assignment = += -= *= /= %= &= ^= |= <<= >>= >>>=
www.SunilOS.com 16
Precedence
Operators have the precedence. Higher
precedence operator will be evaluated before the
lower precedence operator.
o int data = a * b + c ;
since * (multiply) has higher precedence than +
(plus) so a & b will be multiplied first then result will
be added to c.
Expression is equivalent to
o int data = (a * b) + c ;
www.SunilOS.com 17
Unary operators
()()
++++
Group expression
Unary plus
Unary minus----
www.SunilOS.com 18
Unary operators
~~~~
!!!!
++++++++
--------
Bitwise complement
Logical negation
Pre- or Post-increment
Pre- or Post-decrement
www.SunilOS.com 19
Unary operators
i = 0;
count = 2 + i++;
i = 0;
count = 2 + i++;
ii
countcount
11
22
i = 0;
count = 2 + ++i;
i = 0;
count = 2 + ++i;
ii
countcount
11
33
www.SunilOS.com 20
Binary operators
 yyyy
OperandOperand
xxxx
OperandOperand
OperatorOperator
www.SunilOS.com 21
Binary operators
++
--
**
//
Plus
Minus
Multiply
Divide
Remainder
Additive & Multiplicative
%%
www.SunilOS.com 22
Binary operators
== AssignmentAssignment
 Assignment is an binary operator in Java.
 The left-hand operand of an assignment must be an
LVALUE.
 An LVALUE is an expression that refers to a region of
memory.
o Names of variables are LVALUES.
o Names of functions and arrays are NOT LVALUES.
www.SunilOS.com 23
Binary operators
class ExampleAssignment {
public static void main(String[] args) {
int result, val_1, val_2;
result = (val_1 = 1) + (val_2 = 2);
System.out.println("val_1 = "+val_1);
System.out.println("val_2 = "+val_2);
System.out.println("result = "+result);
}
}
class ExampleAssignment {
public static void main(String[] args) {
int result, val_1, val_2;
result = (val_1 = 1) + (val_2 = 2);
System.out.println("val_1 = "+val_1);
System.out.println("val_2 = "+val_2);
System.out.println("result = "+result);
}
}
val_1 = 1
val_2 = 2
result = 3
val_1 = 1
val_2 = 2
result = 3
www.SunilOS.com 24
Binary operators
Expressions involving only integers are
evaluated using integer arithmetic.
float result;
int i,j;
i=25; j=10;
result = i/j;
float result;
int i,j;
i=25; j=10;
result = i/j;
resultresult 2.02.0
www.SunilOS.com 25
Binary operators
Expressions involving only integers are
evaluated using integer arithmetic.
float result;
int i,j;
i=25; j=10;
result = (float) i/j;
float result;
int i,j;
i=25; j=10;
result = (float) i/j;
resultresult 2.52.5
www.SunilOS.com 26
Binary operators
+=+=
-=-=
*=*=
/=/=
Assign sum
Assign difference
Assign product
Assign quotient
Assign remainder%=%=
www.SunilOS.com 27
Binary operators
Compound operators provide a
convenient shorthand.
int i;
i = i + 5;
i += 5;
int i;
i = i + 5;
i += 5;
www.SunilOS.com 28
Binary operators
<<
>>
<=<=
>=>=
Less than
Greater than
Less than or equal to
Greater than or equal to
Equal to
Not equal to
Relational
== ==
!=!=
www.SunilOS.com 29
Binary operators
&&&&
||||
Logical AND
Logical OR
Logical
Expressions connected by && and || are
evaluated from left to right.
www.SunilOS.com 30
Binary operators
Expressions connected by && and || are
evaluated from left to right.
class ExampleAndOr {
public static void main(String[] args) {
int i=0;
System.out.println("Test:" + ((2<3) || (0<i++)));
System.out.println("I:" + i);
}
}
class ExampleAndOr {
public static void main(String[] args) {
int i=0;
System.out.println("Test:" + ((2<3) || (0<i++)));
System.out.println("I:" + i);
}
}
Test:true
I:0
Test:true
I:0
This never gets
evaluated!
This never gets
evaluated!
www.SunilOS.com 31
Bitwise Binary operators
<<
>>
&
^
Shift left
Shift right
Bitwise AND
Bitwise XOR
Bitwise OR
unary bitwise complement
unsigned right shift
|
~
>>>
These operators are less commonly used.
www.SunilOS.com 32
Unary bitwise complement
1 1 11 0 101
~
1 Byte
0 0 00 1 010
byte a = 10;
byte b = ~a;
www.SunilOS.com 33
Left Shift <<
1 1 01 0 101
<<
1 Byte
1 0 10 0 001
byte a = 10;
b = a<<2;
1
1
www.SunilOS.com 34
Right Shift >>
1 1 01 0 101
>>
1 Byte
1 0 10 1 100
byte a = 10;
b = a>>2;
1
0
www.SunilOS.com 35
Unsigned Right Shift >>>
1 1 01 0 101
>>>
1 Byte
0 0 11 1 100
byte a = 10;
b = a>>>2;
1
0
www.SunilOS.com 36
And bitwise &
1 1 01 0 101
&
1 Byte
0 0 11 1 100
byte a = 10;
b = 20;
c = a & b;
0 0 01 0 100
www.SunilOS.com 37
OR bitwise |
1 1 01 0 101
|
1 Byte
0 0 11 1 100
byte a = 10;
b = 20;
c = a | b;
1 1 11 1 101
www.SunilOS.com 38
XOR bitwise ^
1 1 01 0 101
^
1 Byte
0 0 11 1 100
byte a = 10;
b = 20;
c = a ^ b;
1 1 10 1 001
www.SunilOS.com 39
Ternary operators
a?x:ya?x:y
“if a then x, else y”
Conditional
result = (x<y) ? x : y;result = (x<y) ? x : y;
www.SunilOS.com 40
Multiple Assignments
int a = b = c = 10;
www.SunilOS.com 41
Exercise
What is the result of
 int i = 0 ;
 System.out.println(++i + ++i + ++i + ++i + ++i + ++i);
 System.out.println(“” + ++i + ++i + ++i + ++i + ++i + ++i);
www.SunilOS.com 42
Type Conversion
www.SunilOS.com 43
Small to Big data type
Will be done automatically.
oint i = 5;
odouble d = i;
oshort s = 10;
oint i = s;
olong l = i;
www.SunilOS.com 44
Big to Small data type
When precision or data loss likely to happen
then type casting is required.
o double d = 5;
o int i = (int)d;
o short s = (short)i;
o int i = 10;
o float f = (float)i;
www.SunilOS.com 45
Mixing operators
class MixOperator {
public static void main(String[] args) {
char cv;
int iv1 = 64;
cv = (char) iv1;
System.out.println("cv:" + cv);
System.out.println("iv1:" + iv1);
}
}
cv:@
iv1:64
www.SunilOS.com 46
Mixing operators
class MixOperator1 {
public static void main(String[] args) {
double fv1, fv2;
int iv1 = 123;
fv1 = iv1/50;
fv2 = iv1/50.0;
System.out.println("fv1:" + fv1);
System.out.println("fv2:" + fv2);
}
}
fv1:2.0
fv2:2.46
fv1:2.0
fv2:2.46
www.SunilOS.com 47
String to Other data type
String str = “5.5” ;
int i = Integer.parseInt(str);
double d = Double.parseDouble(str);
float f = Float.parseFloat(str);
long l = Long.parseLong(str);
String bStr = “true”;
boolean b = Boolean.parseBoolean(bStr);
www.SunilOS.com 48
Other data type to String
String str = String.valueOf(5);
String str = String.valueOf(5.5);
String str = String.valueOf(true);
String str = String.valueOf(5L);
String str = String.valueOf(5.5D);
Disclaimer
This is an educational presentation to enhance the
skill of computer science students.
This presentation is available for free to computer
science students.
Some internet images from different URLs are
used in this presentation to simplify technical
examples and correlate examples with the real
world.
We are grateful to owners of these URLs and
pictures.
www.SunilOS.com 49
Thank You!
www.SunilOS.com 50
www.SunilOS.com

Weitere ähnliche Inhalte

Was ist angesagt?

Hibernate
Hibernate Hibernate
Hibernate Sunil OS
 
JavaScript
JavaScriptJavaScript
JavaScriptSunil OS
 
Java IO Streams V4
Java IO Streams V4Java IO Streams V4
Java IO Streams V4Sunil OS
 
Resource Bundle
Resource BundleResource Bundle
Resource BundleSunil OS
 
Java Input Output and File Handling
Java Input Output and File HandlingJava Input Output and File Handling
Java Input Output and File HandlingSunil OS
 
Jsp/Servlet
Jsp/ServletJsp/Servlet
Jsp/ServletSunil OS
 
Java Threads and Concurrency
Java Threads and ConcurrencyJava Threads and Concurrency
Java Threads and ConcurrencySunil OS
 
Python Part 1
Python Part 1Python Part 1
Python Part 1Sunil OS
 
String classes and its methods.20
String classes and its methods.20String classes and its methods.20
String classes and its methods.20myrajendra
 
Python part2 v1
Python part2 v1Python part2 v1
Python part2 v1Sunil OS
 
OOP with Java - Continued
OOP with Java - Continued OOP with Java - Continued
OOP with Java - Continued Hitesh-Java
 
Machine learning ( Part 3 )
Machine learning ( Part 3 )Machine learning ( Part 3 )
Machine learning ( Part 3 )Sunil OS
 
Machine learning ( Part 2 )
Machine learning ( Part 2 )Machine learning ( Part 2 )
Machine learning ( Part 2 )Sunil OS
 

Was ist angesagt? (20)

Hibernate
Hibernate Hibernate
Hibernate
 
JDBC
JDBCJDBC
JDBC
 
Log4 J
Log4 JLog4 J
Log4 J
 
JavaScript
JavaScriptJavaScript
JavaScript
 
Java IO Streams V4
Java IO Streams V4Java IO Streams V4
Java IO Streams V4
 
Resource Bundle
Resource BundleResource Bundle
Resource Bundle
 
Java Input Output and File Handling
Java Input Output and File HandlingJava Input Output and File Handling
Java Input Output and File Handling
 
JAVA OOP
JAVA OOPJAVA OOP
JAVA OOP
 
PDBC
PDBCPDBC
PDBC
 
Jsp/Servlet
Jsp/ServletJsp/Servlet
Jsp/Servlet
 
C Basics
C BasicsC Basics
C Basics
 
Java Threads and Concurrency
Java Threads and ConcurrencyJava Threads and Concurrency
Java Threads and Concurrency
 
Python Part 1
Python Part 1Python Part 1
Python Part 1
 
String classes and its methods.20
String classes and its methods.20String classes and its methods.20
String classes and its methods.20
 
DJango
DJangoDJango
DJango
 
C++
C++C++
C++
 
Python part2 v1
Python part2 v1Python part2 v1
Python part2 v1
 
OOP with Java - Continued
OOP with Java - Continued OOP with Java - Continued
OOP with Java - Continued
 
Machine learning ( Part 3 )
Machine learning ( Part 3 )Machine learning ( Part 3 )
Machine learning ( Part 3 )
 
Machine learning ( Part 2 )
Machine learning ( Part 2 )Machine learning ( Part 2 )
Machine learning ( Part 2 )
 

Andere mochten auch

C# Variables and Operators
C# Variables and OperatorsC# Variables and Operators
C# Variables and OperatorsSunil OS
 
Java Swing JFC
Java Swing JFCJava Swing JFC
Java Swing JFCSunil OS
 
Byte stream classes.49
Byte stream classes.49Byte stream classes.49
Byte stream classes.49myrajendra
 
Character stream classes .52
Character stream classes .52Character stream classes .52
Character stream classes .52myrajendra
 
Inner classes9 cm604.28
Inner classes9 cm604.28Inner classes9 cm604.28
Inner classes9 cm604.28myrajendra
 
Nested classes in java
Nested classes in javaNested classes in java
Nested classes in javaRicha Singh
 
Rays Technologies
Rays TechnologiesRays Technologies
Rays TechnologiesSunil OS
 
Inner classes ,annoumous and outer classes in java
Inner classes ,annoumous and outer classes in javaInner classes ,annoumous and outer classes in java
Inner classes ,annoumous and outer classes in javaAdil Mehmoood
 
Various io stream classes .47
Various io stream classes .47Various io stream classes .47
Various io stream classes .47myrajendra
 
Java: Objects and Object References
Java: Objects and Object ReferencesJava: Objects and Object References
Java: Objects and Object ReferencesTareq Hasan
 
Classes and Nested Classes in Java
Classes and Nested Classes in JavaClasses and Nested Classes in Java
Classes and Nested Classes in JavaRavi_Kant_Sahu
 

Andere mochten auch (17)

C# Variables and Operators
C# Variables and OperatorsC# Variables and Operators
C# Variables and Operators
 
Java Swing JFC
Java Swing JFCJava Swing JFC
Java Swing JFC
 
JUnit 4
JUnit 4JUnit 4
JUnit 4
 
Byte stream classes.49
Byte stream classes.49Byte stream classes.49
Byte stream classes.49
 
Character stream classes .52
Character stream classes .52Character stream classes .52
Character stream classes .52
 
Inner classes9 cm604.28
Inner classes9 cm604.28Inner classes9 cm604.28
Inner classes9 cm604.28
 
Nested classes in java
Nested classes in javaNested classes in java
Nested classes in java
 
Rays Technologies
Rays TechnologiesRays Technologies
Rays Technologies
 
Inner classes
Inner classesInner classes
Inner classes
 
Files in java
Files in javaFiles in java
Files in java
 
Inner classes ,annoumous and outer classes in java
Inner classes ,annoumous and outer classes in javaInner classes ,annoumous and outer classes in java
Inner classes ,annoumous and outer classes in java
 
C# Basics
C# BasicsC# Basics
C# Basics
 
Various io stream classes .47
Various io stream classes .47Various io stream classes .47
Various io stream classes .47
 
Java: Objects and Object References
Java: Objects and Object ReferencesJava: Objects and Object References
Java: Objects and Object References
 
C++ oop
C++ oopC++ oop
C++ oop
 
Java Inner Classes
Java Inner ClassesJava Inner Classes
Java Inner Classes
 
Classes and Nested Classes in Java
Classes and Nested Classes in JavaClasses and Nested Classes in Java
Classes and Nested Classes in Java
 

Ähnlich wie JAVA Variables and Operators

03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...Intro C# Book
 
Chapter 2&3 (java fundamentals and Control Structures).ppt
Chapter 2&3 (java fundamentals and Control Structures).pptChapter 2&3 (java fundamentals and Control Structures).ppt
Chapter 2&3 (java fundamentals and Control Structures).ppthenokmetaferia1
 
Data types and Operators
Data types and OperatorsData types and Operators
Data types and Operatorsraksharao
 
Python Basics | Python Tutorial | Edureka
Python Basics | Python Tutorial | EdurekaPython Basics | Python Tutorial | Edureka
Python Basics | Python Tutorial | EdurekaEdureka!
 
03 Operators and expressions
03 Operators and expressions03 Operators and expressions
03 Operators and expressionsmaznabili
 
PE1 Module 2.ppt
PE1 Module 2.pptPE1 Module 2.ppt
PE1 Module 2.pptbalewayalew
 
B.sc CSIT 2nd semester C++ Unit2
B.sc CSIT  2nd semester C++ Unit2B.sc CSIT  2nd semester C++ Unit2
B.sc CSIT 2nd semester C++ Unit2Tekendra Nath Yogi
 
Class_IX_Operators.pptx
Class_IX_Operators.pptxClass_IX_Operators.pptx
Class_IX_Operators.pptxrinkugupta37
 
introduction to python in english presentation file
introduction to python in english presentation fileintroduction to python in english presentation file
introduction to python in english presentation fileRujanTimsina1
 
Python Basics by Akanksha Bali
Python Basics by Akanksha BaliPython Basics by Akanksha Bali
Python Basics by Akanksha BaliAkanksha Bali
 

Ähnlich wie JAVA Variables and Operators (20)

03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
Type Casting Operator
Type Casting OperatorType Casting Operator
Type Casting Operator
 
Chapter 2&3 (java fundamentals and Control Structures).ppt
Chapter 2&3 (java fundamentals and Control Structures).pptChapter 2&3 (java fundamentals and Control Structures).ppt
Chapter 2&3 (java fundamentals and Control Structures).ppt
 
Data types and Operators
Data types and OperatorsData types and Operators
Data types and Operators
 
Welcome to python workshop
Welcome to python workshopWelcome to python workshop
Welcome to python workshop
 
c_tutorial_2.ppt
c_tutorial_2.pptc_tutorial_2.ppt
c_tutorial_2.ppt
 
Python Basics | Python Tutorial | Edureka
Python Basics | Python Tutorial | EdurekaPython Basics | Python Tutorial | Edureka
Python Basics | Python Tutorial | Edureka
 
03 Operators and expressions
03 Operators and expressions03 Operators and expressions
03 Operators and expressions
 
Java unit 3
Java unit 3Java unit 3
Java unit 3
 
PE1 Module 2.ppt
PE1 Module 2.pptPE1 Module 2.ppt
PE1 Module 2.ppt
 
B.sc CSIT 2nd semester C++ Unit2
B.sc CSIT  2nd semester C++ Unit2B.sc CSIT  2nd semester C++ Unit2
B.sc CSIT 2nd semester C++ Unit2
 
Class_IX_Operators.pptx
Class_IX_Operators.pptxClass_IX_Operators.pptx
Class_IX_Operators.pptx
 
introduction to python in english presentation file
introduction to python in english presentation fileintroduction to python in english presentation file
introduction to python in english presentation file
 
Operators
OperatorsOperators
Operators
 
Python programming
Python  programmingPython  programming
Python programming
 
Chapter1.pptx
Chapter1.pptxChapter1.pptx
Chapter1.pptx
 
Python Basics by Akanksha Bali
Python Basics by Akanksha BaliPython Basics by Akanksha Bali
Python Basics by Akanksha Bali
 
CPU
CPUCPU
CPU
 
Aspdot
AspdotAspdot
Aspdot
 

Mehr von Sunil OS

Threads v3
Threads v3Threads v3
Threads v3Sunil OS
 
Exception Handling v3
Exception Handling v3Exception Handling v3
Exception Handling v3Sunil OS
 
Machine learning ( Part 1 )
Machine learning ( Part 1 )Machine learning ( Part 1 )
Machine learning ( Part 1 )Sunil OS
 
Python Pandas
Python PandasPython Pandas
Python PandasSunil OS
 
Angular 8
Angular 8 Angular 8
Angular 8 Sunil OS
 

Mehr von Sunil OS (6)

OOP v3
OOP v3OOP v3
OOP v3
 
Threads v3
Threads v3Threads v3
Threads v3
 
Exception Handling v3
Exception Handling v3Exception Handling v3
Exception Handling v3
 
Machine learning ( Part 1 )
Machine learning ( Part 1 )Machine learning ( Part 1 )
Machine learning ( Part 1 )
 
Python Pandas
Python PandasPython Pandas
Python Pandas
 
Angular 8
Angular 8 Angular 8
Angular 8
 

Kürzlich hochgeladen

Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfDanh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfQucHHunhnh
 
Application of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesApplication of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesRased Khan
 
Navigating the Misinformation Minefield: The Role of Higher Education in the ...
Navigating the Misinformation Minefield: The Role of Higher Education in the ...Navigating the Misinformation Minefield: The Role of Higher Education in the ...
Navigating the Misinformation Minefield: The Role of Higher Education in the ...Mark Carrigan
 
REPRODUCTIVE TOXICITY STUDIE OF MALE AND FEMALEpptx
REPRODUCTIVE TOXICITY  STUDIE OF MALE AND FEMALEpptxREPRODUCTIVE TOXICITY  STUDIE OF MALE AND FEMALEpptx
REPRODUCTIVE TOXICITY STUDIE OF MALE AND FEMALEpptxmanishaJyala2
 
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdfTelling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdfTechSoup
 
2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptxmansk2
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文中 央社
 
Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45
Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45
Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45MysoreMuleSoftMeetup
 
Post Exam Fun(da) Intra UEM General Quiz 2024 - Prelims q&a.pdf
Post Exam Fun(da) Intra UEM General Quiz 2024 - Prelims q&a.pdfPost Exam Fun(da) Intra UEM General Quiz 2024 - Prelims q&a.pdf
Post Exam Fun(da) Intra UEM General Quiz 2024 - Prelims q&a.pdfPragya - UEM Kolkata Quiz Club
 
MichaelStarkes_UncutGemsProjectSummary.pdf
MichaelStarkes_UncutGemsProjectSummary.pdfMichaelStarkes_UncutGemsProjectSummary.pdf
MichaelStarkes_UncutGemsProjectSummary.pdfmstarkes24
 
The Ultimate Guide to Social Media Marketing in 2024.pdf
The Ultimate Guide to Social Media Marketing in 2024.pdfThe Ultimate Guide to Social Media Marketing in 2024.pdf
The Ultimate Guide to Social Media Marketing in 2024.pdfdm4ashexcelr
 
Mbaye_Astou.Education Civica_Human Rights.pptx
Mbaye_Astou.Education Civica_Human Rights.pptxMbaye_Astou.Education Civica_Human Rights.pptx
Mbaye_Astou.Education Civica_Human Rights.pptxnuriaiuzzolino1
 
How to Manage Notification Preferences in the Odoo 17
How to Manage Notification Preferences in the Odoo 17How to Manage Notification Preferences in the Odoo 17
How to Manage Notification Preferences in the Odoo 17Celine George
 
Open Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPointOpen Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPointELaRue0
 
Essential Safety precautions during monsoon season
Essential Safety precautions during monsoon seasonEssential Safety precautions during monsoon season
Essential Safety precautions during monsoon seasonMayur Khatri
 
An Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptxAn Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptxCeline George
 
Morse OER Some Benefits and Challenges.pptx
Morse OER Some Benefits and Challenges.pptxMorse OER Some Benefits and Challenges.pptx
Morse OER Some Benefits and Challenges.pptxjmorse8
 
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17Celine George
 
size separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceuticssize separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceuticspragatimahajan3
 

Kürzlich hochgeladen (20)

Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfDanh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
 
Application of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesApplication of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matrices
 
Navigating the Misinformation Minefield: The Role of Higher Education in the ...
Navigating the Misinformation Minefield: The Role of Higher Education in the ...Navigating the Misinformation Minefield: The Role of Higher Education in the ...
Navigating the Misinformation Minefield: The Role of Higher Education in the ...
 
REPRODUCTIVE TOXICITY STUDIE OF MALE AND FEMALEpptx
REPRODUCTIVE TOXICITY  STUDIE OF MALE AND FEMALEpptxREPRODUCTIVE TOXICITY  STUDIE OF MALE AND FEMALEpptx
REPRODUCTIVE TOXICITY STUDIE OF MALE AND FEMALEpptx
 
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdfTelling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
 
Word Stress rules esl .pptx
Word Stress rules esl               .pptxWord Stress rules esl               .pptx
Word Stress rules esl .pptx
 
2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 
Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45
Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45
Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45
 
Post Exam Fun(da) Intra UEM General Quiz 2024 - Prelims q&a.pdf
Post Exam Fun(da) Intra UEM General Quiz 2024 - Prelims q&a.pdfPost Exam Fun(da) Intra UEM General Quiz 2024 - Prelims q&a.pdf
Post Exam Fun(da) Intra UEM General Quiz 2024 - Prelims q&a.pdf
 
MichaelStarkes_UncutGemsProjectSummary.pdf
MichaelStarkes_UncutGemsProjectSummary.pdfMichaelStarkes_UncutGemsProjectSummary.pdf
MichaelStarkes_UncutGemsProjectSummary.pdf
 
The Ultimate Guide to Social Media Marketing in 2024.pdf
The Ultimate Guide to Social Media Marketing in 2024.pdfThe Ultimate Guide to Social Media Marketing in 2024.pdf
The Ultimate Guide to Social Media Marketing in 2024.pdf
 
Mbaye_Astou.Education Civica_Human Rights.pptx
Mbaye_Astou.Education Civica_Human Rights.pptxMbaye_Astou.Education Civica_Human Rights.pptx
Mbaye_Astou.Education Civica_Human Rights.pptx
 
How to Manage Notification Preferences in the Odoo 17
How to Manage Notification Preferences in the Odoo 17How to Manage Notification Preferences in the Odoo 17
How to Manage Notification Preferences in the Odoo 17
 
Open Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPointOpen Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPoint
 
Essential Safety precautions during monsoon season
Essential Safety precautions during monsoon seasonEssential Safety precautions during monsoon season
Essential Safety precautions during monsoon season
 
An Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptxAn Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptx
 
Morse OER Some Benefits and Challenges.pptx
Morse OER Some Benefits and Challenges.pptxMorse OER Some Benefits and Challenges.pptx
Morse OER Some Benefits and Challenges.pptx
 
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
 
size separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceuticssize separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceutics
 

JAVA Variables and Operators

  • 1. www.SunilOS.com 1 Variables & Operators www.sunilos.com www.raystec.com
  • 2. Variables  Variable stores human data like numbers and alphabets.  Data type will decide what values will be stored in variables.  You can say data type will define the structure of your data. www.SunilOS.com 2
  • 3. Variables and Data Types Decimal values will be stored in float and double data type. Non-decimals values will be stored in int, long, byte, and short data types. Character will be stored in char data type. True/False will be stored in boolean data type. www.SunilOS.com 3
  • 4. www.SunilOS.com 4 Data Types Data types are divided into two categories. Primitive Data Types o byte, short, int, long, float, double, boolean, char. o It occupies number of bytes as per data type. o It stores values. Reference Data Types o It stores memory address of a value. o It occupies 2 bytes to store a reference (memory address). o Strings, Objects, Arrays are reference data types.
  • 5. www.SunilOS.com 5 Primitive Data Types int long byte short float double 1 2 4 8 4 8 -128, +127 -9.223E18, +9.223E18 -32768, +32767 -2147483648, +2147483647 +3.4 E+38 +1.7 E+308 Type Size Byte Range char 2 0, 65535 boolean 1 true, false 0 0 0 0 0 0 0 false Default
  • 6. www.SunilOS.com 6 Declare Variable int total; total = 5+6; short srt; srt = 3; char ch; ch =‘A’ ; 4 Bytes Stack Memory1001 total 1001 11 2 Bytes 1011 3 srt 1011 2 Bytes 1010 A ch 1010
  • 7. www.SunilOS.com 7 Declare Object  int total;  total = 5+6;  String str;  str = “sunRays”; o or  str = new String(“sunRays”); 4 Bytes Stack Memory 1001 total 1001 11 2 Bytes 1011 1010 str 1011 14 Bytes 1010 SUNRAYS
  • 8. www.SunilOS.com 8 Declare Variable – Primitive Data  int total;  total = 5+6;  int newTotal;  newTotal = total ;  newTotal = newTotal+3 ; 4 Bytes Stack Memory1001 total 1001 11 4 Bytes 1011 newTotal 1011 1114
  • 9. www.SunilOS.com 9 Declare Object - Copy reference  String str;  str = “sunRays”  Or  str = new String(“sunRays”)  String newStr;  newStr = str; Stack Memory 2 Bytes 1011 1010 str 1011 14 Bytes 1010 SUNRAYS 2 Bytes 1110 1010 newStr 1110
  • 10. Java Identifier It is a name of: o Variable o Method o Class o Interface o Package Used to identify a variable, method and class in its scope. www.SunilOS.com 10
  • 11. www.SunilOS.com 11 Java Identifier Rules  Name of an Identifier follows certain rules. Here are key rules: o The first character must be a non-digit character from the Unicode standard String firstName; o Subsequent characters may include digits int total123 ; o Java is case sensitive that means Character Case is significant • int count =0 ; Count = 1 ; are two different variables o Avoid using underscore (_) and $ for the first character. o User-defined identifiers can not duplicate Java keywords.
  • 12. www.SunilOS.com 12 What’s an operator? Operators are tokens that trigger some computation when applied to variables and other objects. It can be categorized into: o Arithmetic o logical o bit-level and o Class access operators.
  • 13. www.SunilOS.com 13 Java operators () ++ -- ! ~ instance of * / % + - << >> >>> < > <= >= == != & ^ | && || ?: = op=
  • 14. www.SunilOS.com 14 Operator Precedence int a = 2+ 4 + 8; int a = 2+ 4 * 8; int a = b = c = 5;
  • 15. www.SunilOS.com 15 Operator Precedence Operators Precedence postfix expr++ expr-- unary ++expr --expr +expr -expr ~ ! multiplicative * / % additive + - shift << >> >>> relational < > <= >= instanceof equality == != bitwise AND & bitwise exclusive OR ^ bitwise inclusive OR | logical AND && logical OR || conditional ? : assignment = += -= *= /= %= &= ^= |= <<= >>= >>>=
  • 16. www.SunilOS.com 16 Precedence Operators have the precedence. Higher precedence operator will be evaluated before the lower precedence operator. o int data = a * b + c ; since * (multiply) has higher precedence than + (plus) so a & b will be multiplied first then result will be added to c. Expression is equivalent to o int data = (a * b) + c ;
  • 17. www.SunilOS.com 17 Unary operators ()() ++++ Group expression Unary plus Unary minus----
  • 18. www.SunilOS.com 18 Unary operators ~~~~ !!!! ++++++++ -------- Bitwise complement Logical negation Pre- or Post-increment Pre- or Post-decrement
  • 19. www.SunilOS.com 19 Unary operators i = 0; count = 2 + i++; i = 0; count = 2 + i++; ii countcount 11 22 i = 0; count = 2 + ++i; i = 0; count = 2 + ++i; ii countcount 11 33
  • 20. www.SunilOS.com 20 Binary operators  yyyy OperandOperand xxxx OperandOperand OperatorOperator
  • 22. www.SunilOS.com 22 Binary operators == AssignmentAssignment  Assignment is an binary operator in Java.  The left-hand operand of an assignment must be an LVALUE.  An LVALUE is an expression that refers to a region of memory. o Names of variables are LVALUES. o Names of functions and arrays are NOT LVALUES.
  • 23. www.SunilOS.com 23 Binary operators class ExampleAssignment { public static void main(String[] args) { int result, val_1, val_2; result = (val_1 = 1) + (val_2 = 2); System.out.println("val_1 = "+val_1); System.out.println("val_2 = "+val_2); System.out.println("result = "+result); } } class ExampleAssignment { public static void main(String[] args) { int result, val_1, val_2; result = (val_1 = 1) + (val_2 = 2); System.out.println("val_1 = "+val_1); System.out.println("val_2 = "+val_2); System.out.println("result = "+result); } } val_1 = 1 val_2 = 2 result = 3 val_1 = 1 val_2 = 2 result = 3
  • 24. www.SunilOS.com 24 Binary operators Expressions involving only integers are evaluated using integer arithmetic. float result; int i,j; i=25; j=10; result = i/j; float result; int i,j; i=25; j=10; result = i/j; resultresult 2.02.0
  • 25. www.SunilOS.com 25 Binary operators Expressions involving only integers are evaluated using integer arithmetic. float result; int i,j; i=25; j=10; result = (float) i/j; float result; int i,j; i=25; j=10; result = (float) i/j; resultresult 2.52.5
  • 26. www.SunilOS.com 26 Binary operators +=+= -=-= *=*= /=/= Assign sum Assign difference Assign product Assign quotient Assign remainder%=%=
  • 27. www.SunilOS.com 27 Binary operators Compound operators provide a convenient shorthand. int i; i = i + 5; i += 5; int i; i = i + 5; i += 5;
  • 28. www.SunilOS.com 28 Binary operators << >> <=<= >=>= Less than Greater than Less than or equal to Greater than or equal to Equal to Not equal to Relational == == !=!=
  • 29. www.SunilOS.com 29 Binary operators &&&& |||| Logical AND Logical OR Logical Expressions connected by && and || are evaluated from left to right.
  • 30. www.SunilOS.com 30 Binary operators Expressions connected by && and || are evaluated from left to right. class ExampleAndOr { public static void main(String[] args) { int i=0; System.out.println("Test:" + ((2<3) || (0<i++))); System.out.println("I:" + i); } } class ExampleAndOr { public static void main(String[] args) { int i=0; System.out.println("Test:" + ((2<3) || (0<i++))); System.out.println("I:" + i); } } Test:true I:0 Test:true I:0 This never gets evaluated! This never gets evaluated!
  • 31. www.SunilOS.com 31 Bitwise Binary operators << >> & ^ Shift left Shift right Bitwise AND Bitwise XOR Bitwise OR unary bitwise complement unsigned right shift | ~ >>> These operators are less commonly used.
  • 32. www.SunilOS.com 32 Unary bitwise complement 1 1 11 0 101 ~ 1 Byte 0 0 00 1 010 byte a = 10; byte b = ~a;
  • 33. www.SunilOS.com 33 Left Shift << 1 1 01 0 101 << 1 Byte 1 0 10 0 001 byte a = 10; b = a<<2; 1 1
  • 34. www.SunilOS.com 34 Right Shift >> 1 1 01 0 101 >> 1 Byte 1 0 10 1 100 byte a = 10; b = a>>2; 1 0
  • 35. www.SunilOS.com 35 Unsigned Right Shift >>> 1 1 01 0 101 >>> 1 Byte 0 0 11 1 100 byte a = 10; b = a>>>2; 1 0
  • 36. www.SunilOS.com 36 And bitwise & 1 1 01 0 101 & 1 Byte 0 0 11 1 100 byte a = 10; b = 20; c = a & b; 0 0 01 0 100
  • 37. www.SunilOS.com 37 OR bitwise | 1 1 01 0 101 | 1 Byte 0 0 11 1 100 byte a = 10; b = 20; c = a | b; 1 1 11 1 101
  • 38. www.SunilOS.com 38 XOR bitwise ^ 1 1 01 0 101 ^ 1 Byte 0 0 11 1 100 byte a = 10; b = 20; c = a ^ b; 1 1 10 1 001
  • 39. www.SunilOS.com 39 Ternary operators a?x:ya?x:y “if a then x, else y” Conditional result = (x<y) ? x : y;result = (x<y) ? x : y;
  • 41. www.SunilOS.com 41 Exercise What is the result of  int i = 0 ;  System.out.println(++i + ++i + ++i + ++i + ++i + ++i);  System.out.println(“” + ++i + ++i + ++i + ++i + ++i + ++i);
  • 43. www.SunilOS.com 43 Small to Big data type Will be done automatically. oint i = 5; odouble d = i; oshort s = 10; oint i = s; olong l = i;
  • 44. www.SunilOS.com 44 Big to Small data type When precision or data loss likely to happen then type casting is required. o double d = 5; o int i = (int)d; o short s = (short)i; o int i = 10; o float f = (float)i;
  • 45. www.SunilOS.com 45 Mixing operators class MixOperator { public static void main(String[] args) { char cv; int iv1 = 64; cv = (char) iv1; System.out.println("cv:" + cv); System.out.println("iv1:" + iv1); } } cv:@ iv1:64
  • 46. www.SunilOS.com 46 Mixing operators class MixOperator1 { public static void main(String[] args) { double fv1, fv2; int iv1 = 123; fv1 = iv1/50; fv2 = iv1/50.0; System.out.println("fv1:" + fv1); System.out.println("fv2:" + fv2); } } fv1:2.0 fv2:2.46 fv1:2.0 fv2:2.46
  • 47. www.SunilOS.com 47 String to Other data type String str = “5.5” ; int i = Integer.parseInt(str); double d = Double.parseDouble(str); float f = Float.parseFloat(str); long l = Long.parseLong(str); String bStr = “true”; boolean b = Boolean.parseBoolean(bStr);
  • 48. www.SunilOS.com 48 Other data type to String String str = String.valueOf(5); String str = String.valueOf(5.5); String str = String.valueOf(true); String str = String.valueOf(5L); String str = String.valueOf(5.5D);
  • 49. Disclaimer This is an educational presentation to enhance the skill of computer science students. This presentation is available for free to computer science students. Some internet images from different URLs are used in this presentation to simplify technical examples and correlate examples with the real world. We are grateful to owners of these URLs and pictures. www.SunilOS.com 49