You are on page 1of 19

Java:Basics

Java Keywords
The following list shows the reserved words in Java. These
reserved words may not be used as constant or variable or
any other identifier names.
Comments in Java
Java supports single-line and multi-line comments very
similar to C and C++. All characters available inside any
comment are ignored by Java compiler.
public class MyFirstJavaProgram{
/* This is my first java program.
* This will print 'Hello World' as the output
* This is an example of multi-line comments.
*/
public static void main(String []args){
// This is an example of single line comment
/* This is also an example of single line comment. */
System.out.println("Hello World");
}
}
Printing Text in Java
Output statement is used to display a prompt or message and
show results. In Java, output on the standard output device (the
monitor) is accomplished by using the standard output object
System.out. The object System.out has access to two methods,
print and println, to output a string on the monitor.
System.out.print(argument);
System.out.println(argument);
print – leaves the insertion point after the last
character of the value of expression.
println – positions the insertion point at the beginning
of the next line.
Printing text in Java
print – leaves the insertion point after the last character of the
value of expression.
println – positions the insertion point at the beginning of the
next line.
Java Data Types
Data types – is a set of values together with a set of operations
allowed on those values.
Primitive Types
There are eight primitive datatypes supported by Java.
Primitive datatypes are predefined by the language and named
by a keyword. Let us now look into the eight primitive data
types in detail.
•byte
•short
•int
•long
•float
•double
•boolean
•char
Java Operators
Java provides a rich set of operators to manipulate
variables. We can divide all the Java operators into
the following groups:
Arithmetic Operators
Relational Operators
Bitwise Operators
Logical Operators
Assignment Operators
Misc Operators
The Arithmetic Operators
Arithmetic operators are used in mathematical expressions in the same way that
they are used in algebra. The following table lists the arithmetic operators −
Assume integer variable A holds 10 and variable B holds 20, then:
Relational Operators
Assume variable A holds 10 and variable B holds 20, then:
The Logical Operators
Assume Boolean variables A holds true and variable B holds false, then:
Example:
public class Test {
public static void main(String args[]) {
boolean a = true;
boolean b = false;
System.out.println("a&&b = " + (a&&b)); System.out.println("a||b = " +
(a||b) ); System.out.println("!(a && b) = " + !(a && b));
}
}
Assignment Operators
Assignment Operators
Java Input
There are several ways to get input from the user in
Java. You will learn to get input by using Scanner
object.
First, you need to import Scanner class using:

import java.util.Scanner;
Accepting Input
Then, we will create an object of Scanner class which will
be used to get input from the user.

Scanner input = new Scanner(System.in);


int number = input.nextInt();
Example: Get Integer Input From
the User
import java.util.Scanner;
public class Input {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
int number = input.nextInt();
System.out.println("You entered " + number);
}
}
Scanner Methods
To get long, float, double and String input from the user,
you can use nextLong(), nextFloat(), nextDouble() and
next() methods respectively.

You might also like