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

L2 Basic IO, Operators

Uploaded by

Siddharth Jha
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

L2 Basic IO, Operators

Uploaded by

Siddharth Jha
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Basic I/O, Operators

PRINTING
There are three standard streams, all are managed by the
java.lang.System class
• Standard input--referenced by System.in
Used for program input, typically reads input entered by the user.
• Standard output--referenced by System.out
Used for program output, typically displays information to the user.
• Standard error--referenced by System.err
Used to display error messages to the user.
The basic output statement is :
System.out.println( );

Others methods:
1. System.out.println()
2. System.out.print()
3. System.out.printf()
Let us see an example

class AssignmentOperator
{
public static void main(String[] args)
{
System.out.println("Java programming.");
}
}
Difference between print(), println() and printf()

 print() - prints string inside the quotes.

 println() - prints string inside the quotes similar


like print() method. Then the cursor moves to the
beginning of the next line.

 printf() - it provides string formatting.


Guess the output

class Output
{
public static void main(String[] args)
{
System.out.println("1. println ");
System.out.println("2. println ");
System.out.print("1. print ");
System.out.print("2. print");
}
}
PRINTING VARIABLES AND LITERALS
To display integers, variables and so on, do not use quotation marks.

class Variables
{
public static void main(String[] args)
{
Double number = -10.6;
System.out.println(5);
System.out.println(number);
}
}
Print concatenated strings
You can use + operator to concatenate strings and print it.

class PrintVariables
{
public static void main(String[] args)
{
Double number = -10.6;
System.out.println("I am " + "awesome.");
System.out.println("Number = " + number);
}
}
READING INPUT FROM CONSOLE

In Java, there are three different ways for reading input


from the user in the command line environment(console).

1. Using Buffered Reader Class


2. Using Scanner Class
3. Using Console Class
BUFFERED READER CLASS
This method is used by wrapping the System.in
(standard input stream) in an InputStreamReader
which is wrapped in a BufferedReader, we can read
input from the user in the command line.
BUFFERED READER CLASS

import java.io.BufferedReader; // Reading data using readLine


import java.io.IOException; String name = reader.readLine();
import java.io.InputStreamReader;
public class Test // Printing the read line
{ System.out.println(name);
public static void main(String[] args) }
throws IOException }
{
//Enter data using BufferReader
BufferedReader reader =
new BufferedReader(new
InputStreamReader(System.in));
SCANNER CLASS

 This is probably the most preferred method to take


input.

 The main purpose of the Scanner class is to parse


primitive types and strings using regular expressions,
however it is also can be used to read input from the
user in the command line.
SCANNER CLASS
import java.util.Scanner; int a = in.nextInt();
System.out.println("You entered
class GetInputFromUser integer "+a);
{
public static void main(String args[]) float b = in.nextFloat();
{ System.out.println("You entered
// Using Scanner for Getting Input from User float "+b);
}
Scanner in = new Scanner(System.in); }
String s = in.nextLine();
System.out.println("You entered string "+s);
CONSOLE CLASS

 It has been becoming a preferred way for reading


user’s input from the command line.

 In addition, it can be used for reading password-like


input without echoing the characters entered by the
user; the format string syntax can also be used (like
System.out.printf()).
CONSOLE CLASS
public class Sample
{
public static void main(String[] args)
{
// Using Console to input data from user
String name = System.console().readLine();

System.out.println(name);
}
}
Java Variables
A variable is a container which holds the value while the Java program is executed.
A variable is assigned with a data type. Variable is a name of memory location.
3
Example to understand the types of variables in java

public class A
{
static int m=100;//static variable
void method()
{
int n=90;//local variable
}
public static void main(String args[])

{
int data=50;//instance variable
}
}//end of class
Data Types in Java
Data types specify the different sizes and values that can be stored
in the variable.

There are two types of data types in Java:

1.Primitive data types: The primitive data types include boolean,


char, byte, short, int, long, float and double.

2.Non-primitive data types: The non-primitive data types


include Classes, Interfaces, and Arrays.
Data Default Value Default size
Type
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
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 == !=

Bitwise bitwise AND &

bitwise exclusive OR ^

bitwise inclusive OR |

Logical logical AND &&

logical OR ||

Ternary ternary ?:

Assignment assignment = += -= *= /= %= &= ^= |= <<= >>=


>>>=
Java Unary Operator
The Java unary operators require only one operand.
Unary operators are used to perform various operations i.e.:
•incrementing/decrementing a value by one
•negating an expression
•inverting the value of a boolean
public class OperatorExample{
public static void main(String args[])
{
int x=10;
System.out.println(x++);//10 (11)
System.out.println(++x);//12
System.out.println(x--);//12 (11)
System.out.println(--x);//10
}}
public class OperatorExample
{
public static void main(String args[])
{
int a=10;
int b=10;
System.out.println(a++ + ++a);
System.out.println(b++ + b++);
}
}
public class OperatorExample
{
public static void main(String args[])
{
int a=10;
int b=-10;
boolean c=true;
boolean d=false;
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 Java Right Shift
Operator Operator
public class OperatorExample public OperatorExample
{ {
public static void main(String args[]) public static void main(String args[])
{ {
System.out.println(10<<2);//10*2^2=10*4=40 System.out.println(10>>2);//10/2^2=10/4=2
System.out.println(10<<3);//10*2^3=10*8=80 System.out.println(20>>2);//20/2^2=20/4=5
System.out.println(20<<2);//20*2^2=20*4=80 System.out.println(20>>3);//20/2^3=20/8=2
System.out.println(15<<4);// }
15*2^4=15*16=24 }
}
}
Logical && and Bitwise &
public 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);//10 because second condition is not checked
System.out.println(a<b&a++<c);//false && true = false
System.out.println(a);//11 because second condition is checked
}}
Logical || and Bitwise |

public 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);//10 because second condition is not checked
System.out.println(a>b|a++<c);//true | true = true
System.out.println(a);//11 because second condition is checked
}}
Java Ternary Operator

public class OperatorExample{


public static void main(String args[])
{
int a=10;
int b=5;
int min=(a<b)?a:b;
System.out.println(min);
}}
Java Assignment Operator
public class OperatorExample{
public static void main(String[] args)
{
int a=10;
a+=3;//10+3
System.out.println(a);
a-=4;//13-4
System.out.println(a);
a*=2;//9*2
System.out.println(a);
a/=2;//18/2
System.out.println(a);
}}
List of Java
Keywords
abstract else interface strictfp
boolean enum long super
break extends native switch
byte final new synchronized
case finally null this
Catch float package throw
Char for private throws
class if protected transient
Continue implements public try
Default import return void
Do instanceof short volatile
Double int static while
THANK YOU

You might also like