0% found this document useful (0 votes)
16 views

2 Java Basic DataTypes - Operators - Input

java basics

Uploaded by

darienvalmeo2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

2 Java Basic DataTypes - Operators - Input

java basics

Uploaded by

darienvalmeo2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

• Data Types

• 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

n Four integer types:


q byte, short, int, long
n Two floating point types
q float, double
n One of them is for characters
q char
n One of them is for boolean values
q boolean

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

byte byte a = 100 , byte b = -50

short short s= 10000 , short r = -20000

int int a = 100000, int b = -200000

long int a = 100000L, int b = -200000L

float float f1 = 234.5f

double double d1 = 123.4

boolean boolean one = true


char char letterA ='A'

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.

String myStr = new String();

Scanner scan = new Scanner(System.in);

int[] scores = {100,99,20,56};

BULSU CICT 7
BULSU CICT 8
Operators:

§ Arithmetic operators
§ Relational and conditional operators
§ Logical operators
§ Assignment operators

BULSU CICT 9
Expressions and Assignment

n An e xp re ssio n is a combination of one or


more o p e ra to rs and o p e ra n d s
n Arithmetic operators: +, -, *, /, %
q Use the normal order of operations
e.g. int exp = 2 * 5 +7;
count = count + 1;
count++;
n Boolean operators: &&, ||

10
Relational Operators

Operator Description Example

== Is equal to 10==20 = false

!= Not equal to 10!=20 = true

> Greater than 20>10 = true

>= Greater than or equal to 20>=10 = true

< Less than 20<10 = false

<= Less than or equal to 20<=10 = false

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

n What is the value of a, b & c


n Done right to left: a = (b = c);

BULSU CICT 13
Basic Mathematical Operators

are the mathematical operators


have a higher precedence than or
double myValue = a + b % d – c * d / b;

n Is the same as:

BULSU CICT 14
11
public class MainClass {

public static void main(String[] argv) {


int count = 10;
++count;

System.out.println(count--);
}

BULSU CICT 15
Decrement and Increment Operators

public class MainClass {


public static void main(String[] args) {
int numA = 5;
int numB = 10;
int numC = 0;

numC = ++numA + numB;

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);

// arithmetic using doubles


System.out.println("\nFloating Point Arithmetic");
d o u b le da = 1 + 1;
d o u b le db = da * 3;
d o u b le dc = db / 4;
d o u b le dd = dc - a;
d o u b le de = -dd;
System.out.println("da = " + da);
System.out.println("db = " + db);
System.out.println("dc = " + dc);
System.out.println("dd = " + dd);
System.out.println("de = " + de);}}

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:

nextInt() Scans the next token of the input as an int.

nextDouble() Scans the next token of the input as a double.

nextFloat() Scans the next token of the input as a float.


Advances this scanner past the current line and
nextLine()
returns the input that was skipped.
nextLong() Scans the next token of the input as a long.

nextShort() Scans the next token of the input as a short.

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.

read() Reads a single character.

readLine() Reads a line of text.

BULSU CICT 22
Buffered Reader: java.io

BULSU CICT 23

You might also like