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

Basic Syntax

Uploaded by

Aditya Kaushal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Basic Syntax

Uploaded by

Aditya Kaushal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

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.

 The individual words, symbols and punctuation marks


presented by tokens.
 Ex:

 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:

5,734 150.36 187-


Floating point constants
 Any number written with one decimal point is called

floating point constant.


Valid:
-0.145 0.98 2.67
Invalid:
153 12.256.32 5/3 1,253,253,56
Character constants
 A character constant consists of a character or string

enclosed with in quotation marks.


Types:
 Single character constant

 String character constant


Single character constant
 A single character constant consists of a single character
enclosed with in quotation marks.
Ex:
char a=‘A’;
String character constant
 A string character constant consists of a sequence of a
character enclosed with in double quotation marks.
Ex:
string s=“Java”
Variable
 A variable is a container which holds the value while
the java program is executed.
 A variable is assigned with a data type.
Declaration of variable in java
Syntax:
 type variable name;

 type variable name1, variable name2, variable name n;

Example:
 int width, height;

 char letter;

 float age, area;

 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

method, is called instance variable.

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

all the instances of the class.


local variable
 A variable declared inside the body of the method is called local
variable.
 A local variable cannot be defined with "static" keyword.

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.

Range of values that can be


Type Size
stored

byte 1 byte −128 to 127

short 2 bytes −32768 to 32767

int 4 bytes −2,147,483,648 to 2,147,483,647

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

 input object of Scanner class is created.

 the nextInt() method of the Scanner class is used to get

integer input from the user.


 To get long, float, double and String input from the

user, you can use,


nextLong(), nextFloat(), nextDouble() and next() met
hods respectively.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy