2 Java Basic DataTypes - Operators - Input
2 Java Basic DataTypes - Operators - Input
• Operators
• Input Methods
1
Java Data Types
P rim itiv e D a ta Ty p e s
-Primitive data types are predefined by the
language and named by a key word.
R e fe re n c e /O b je c t D a ta Ty p e s
- The object data type allows you to define a Java
object.
BULSU CICT 2
Primitive Data Types in Java
3
Declaring Variables
The Java variable declaration creates a new variable with required properties. The programming language requires four basic things to declare a variable in the program.
n Syntax:
Data_type variable_name = value;
1 2 3
Examples:
int count1, int count 2;
int count = 0;
String studentName = “Raiqa
Reyes”;
char letterA = ‘A’;
4
Primitive or Value Data types:
Data Types Example
BULSU CICT 5
Primitive Data types:
n Syntax:
Data_type variable_name = value;
int value1 = 0;
double totalAmt = 0.00;
boolean flag = false;
float disct = 0.0;
char charVal = ‘’;
BULSU CICT 6
Reference or Object Data types:
Variables of reference types store references to their data
(objects), while variables of value types directly contain their
data.
BULSU CICT 7
BULSU CICT 8
Operators:
§ Arithmetic operators
§ Relational and conditional operators
§ Logical operators
§ Assignment operators
BULSU CICT 9
Expressions and Assignment
10
Relational Operators
BULSU CICT 11
Logical Operators
Assum e Boolean variables A holds true and variable B holds false, then:
BULSU CICT 12
Assignment
n All Java assignments are right associative
BULSU CICT 13
Basic Mathematical Operators
BULSU CICT 14
11
public class MainClass {
System.out.println(count--);
}
BULSU CICT 15
Decrement and Increment Operators
System.out.println(numA);
System.out.println(numC);
}
}
Output:
BULSU CICT 16
p u b lic class MainClass {
p u b lic static vo id main(String args[]) {
// arithmetic using integers
System.out.println("Integer Arithmetic");
in t a = 1 + 1;
in t b = a * 3;
in t c = b / 4;
in t d = c - a;
in t e = - d;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("d = " + d);
System.out.println("e = " + e);
BULSU CICT 17
More Assignment Operators
n x += y is equivalent to x = x + y
n Also:
q -=
q *=
q /=
q %=
18
BULSU CICT 19
Java Scanner class allows the user to take input from the console. It
belongs to java.u til p ackag e. It is used to read the input of primitive
types like int, double, long, short, float, and byte. It is the easiest way to
read input in Java program.
Methods:
BULSU CICT 20
Scanner
BULSU CICT 21
Buffered Reader: java.io
Java BufferedReader class is used to read the text from a character-
based input stream. It can be used to read data line by line by
readLine() method. It makes the performance fast. It inherits Reader
class.
BULSU CICT 22
Buffered Reader: java.io
BULSU CICT 23