L2 Basic IO, Operators
L2 Basic IO, 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()
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
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.
additive +-
equality == !=
bitwise exclusive OR ^
bitwise inclusive OR |
logical OR ||
Ternary ternary ?: