Basic Syntax
Basic Syntax
Basic Syntax
Syntax
In every program must be followed by syntax rules.
Tokens
Semicolon
Identifiers
Keywords
Constants
Variables
Tokens
Smallest individual units in a program are known as Tokens.
class Sample
{
int a,b,c;
}
Tokens are:
class
Sample
{
int
a
,
b
,
c
;
}
Semicolon
In a program, the semicolon is a statement terminator.
That is, each individual statement must be ended with a semicolon.
Ex:
class Add
{
public static void main(String[] args)
{
int a=10,b=20,c;
c=a+b;
System.out.println(c);
}
}
Identifiers
Identifiers are names given to the variables,methods,arrays
and other user defined object.
These are user defined names.
Rules:
Identifiers are formed with alphabets, digits, and a special
character underscore(_).
The first character must be an alphabet.
No special characters are allowed other than underscore(_)
These are case sensitive.
Ex :
AA and aa are different identifiers
Keywords
Java language provides 50 keywords.
Keywords are the reserved words these meaning are already
known t the compiler.
You cannot use it as a variable name, constant name etc.
Constants
The value does not change during execution in a program.
There are Two types of constants in java
Numeric constants
Character constants
Numeric constants
A Numeric constants is a constants made up of digits and
some special symbols.
Integer constants
floating point constants
Integer constants
Integer constant is a constant made up of digits without
decimal point.
Valid:
10 -542 5782
Invalid:
Example:
int width, height;
char letter;
double d;
Types of Variables
There are three types of variables in java:
instance variable
static variable
local variable
instance variable
A variable declared inside the class but outside the body of the
static variable
A variable which is declared as static is called static variable.
It cannot be local.
You can create a single copy of static variable and share among
Ex:
class A
{
int data=50;//instance variable
static int m=100;//static variable
public static void main(String args[])
{
int n=90;//local variable
}
}
Data type
Data type specifies the size and type of values that can be
stored in an variable.
There are two types of data types in Java:
Primitive data types:
The primitive data types include boolean , char, int , float
and double.
Non-primitive data types:The non-primitive data types
include Classes, Interfaces, and Arrays.
Integer
Integer types can hold whole numbers such as 123 and −96.
9,223,372,036,854,775,808 to
long 8 bytes 9,223,372,036,854,755,807
class JavaDatatype
{
public static void main(String[] args)
{
byte num=113;
short num1=150;
int num2=100000;
long num3=12332252626L;
System.out.println(num);
System.out.println(num1);
System.out.println(num2);
System.out.println(num3);
}
}
Floating Point
Floating point data types are used to represent numbers with
a fractional part.
Range of values
Type Size that can be
stored
3.4e−038 to
float 4 bytes
3.4e+038
1.7e−308 to
double 8 bytes 1.7e+038
class JavaExample
{
public static void main(String[] args)
{
float num = 19.98f;
double num1 = -42937737.9d;
System.out.println(num);
System.out.println(num1);
}
}
Character
The char data type is used to store characters.
size: 2 bytes.
Ex:
class JavaExample
{
public static void main(String[] args)
{
char ch = 'Z';
System.out.println(ch);
}
}
Boolean
The Boolean data type is used to store only two possible values:
true and false.
size: 1 bit.
Ex:
class JavaExample
{
public static void main(String[] args)
{
boolean b = false;
System.out.println(b);
}
}
input/output in java
Java Output
use System.out.println(), System.out.print() to send output to
standard output (screen).
Ex:
class Output
{
public static void main(String[] args)
{
System.out.print(“Welcome ");
System.out.println(“Java");
System.out.print(“Language”);
}
}
Java Input:
There are several ways to get input from the user in Java.
You will get input by using Scanner object.
For that, you need to import Scanner class using:
import java.util.Scanner;
we will create an object of Scanner class which will be used
to get input from the user.
Ex:
Scanner input = new Scanner(System.in);
int number = input.nextInt();
Ex:
import java.util.Scanner;
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);
}
}
Where