The document discusses variables and operators in Java. It defines variables as containers that store data like numbers and letters, and notes that data types determine what values can be stored. It then covers various data types including primitive types like int and float, and reference types like Strings and Arrays. The document also discusses operators like arithmetic, logical, and assignment operators. It provides examples of declaring and using variables and operators in Java code.
Introduction to the presentation content, focusing on variables and operators.
Variables store human data and are defined by data types. Data types include float, int, char, and boolean.
Data types are classified into primitive (e.g., int, float) and reference types (e.g., String, Arrays).
How to declare variables and instantiate primitive data types, with examples of total and newTotal. Declaring and initializing objects, specifically Strings, and copying their references.
Defining identifiers in Java, including rules about naming variables, methods, and classes.
Introduction to operators as computation triggers, including categories like arithmetic and logical.
Explains the precedence of operators in expressions and the evaluation order.
Details on unary operators such as increment and decrement, with examples.
Analyzes binary operators including arithmetic, comparative, and logical operators with examples.
Covers bitwise operators, their functions, and includes examples of left shift and bitwise operations.Explains the ternary operator and how multiple assignments work in Java.
Details on type conversion, including automatic conversion and explicit casting for different data types.
Demonstration of mixing operator types in Java, showcasing implications in resulting values.
How to convert Strings to other data types and vice versa, using parse methods.
End of the presentation with a disclaimer about educational intent and gratitude for image sources.
Variables
Variable storeshuman 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 DataTypes
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
Datatypes 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 DataTypes
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
Java Identifier
It isa 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 IdentifierRules
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 anoperator?
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 16
Precedence
Operators havethe 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 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
classExampleAssignment {
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
Expressionsinvolving 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
Expressionsinvolving 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 30
Binary operators
Expressionsconnected 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 Binaryoperators
<<
>>
&
^
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 43
Small toBig 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 toSmall 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 47
String toOther 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);
Disclaimer
This is aneducational 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