2.
Basic of 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.
Variable is a name of memory location. There are three types of variables in java: local, instance and static.
There are two types of data types in Java: primitive and non-primitive.
Variable
Variable is name of reserved area allocated in memory. In other words, it is a name of memory location. It is a
combination of "vary + able" that means its value can be changed.
int data=50;//Here data is variable
Types of Variables
There are three types of variables in Java:
local variable
instance variable
static variable
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:
Primitive data types: The primitive data types include boolean, char, byte, short, int, long, float and double.
Non-primitive data types: The non-primitive data types include Classes, Interfaces, and Arrays.
Java Primitive Data Types
In Java language, primitive data types are the building blocks of data manipulation. These are the most basic data
types available in Java language
There are 8 types of primitive data types:
Boolean data type
byte data type
char data type
short data type
int data type
long data type
float data type
double data type
Data Type Default Value Default size
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
Boolean Data Type
The Boolean data type is used to store only two possible values: true and false. This data type is used for simple
flags that track true/false conditions.
The Boolean data type specifies one bit of information, but its "size" can't be defined precisely.
Example: Boolean one = false
Byte Data Type
The byte data type is an example of primitive data type. It is an 8-bit signed two's complement integer. Its value-
range lies between -128 to 127 (inclusive). Its minimum value is -128 and maximum value is 127. Its default value
is 0.
The byte data type is used to save memory in large arrays where the memory savings is most required. It saves
space because a byte is 4 times smaller than an integer. It can also be used in place of "int" data type.
Example: byte a = 10, byte b = -20
Short Data Type
The short data type is a 16-bit signed two's complement integer. Its value-range lies between -32,768 to 32,767
(inclusive). Its minimum value is -32,768 and maximum value is 32,767. Its default value is 0.
The short data type can also be used to save memory just like byte data type. A short data type is 2 times smaller
than an integer.
Example: short s = 10000, short r = -5000
Int Data Type
The int data type is a 32-bit signed two's complement integer. Its value-range lies between - 2,147,483,648 (-2^31)
to 2,147,483,647 (2^31 -1) (inclusive). Its minimum value is - 2,147,483,648and maximum value is 2,147,483,647.
Its default value is 0.
The int data type is generally used as a default data type for integral values unless if there is no problem about
memory.
Example: int a = 100000, int b = -200000
Long Data Type
The long data type is a 64-bit two's complement integer. Its value-range lies between -9,223,372,036,854,775,808(-
2^63) to 9,223,372,036,854,775,807(2^63 -1)(inclusive). Its minimum value is - 9,223,372,036,854,775,808and
maximum value is 9,223,372,036,854,775,807. Its default value is 0. The long data type is used when you need a
range of values more than those provided by int.
Example: long a = 100000L, long b = -200000L
Float Data Type
The float data type is a single-precision 32-bit IEEE 754 floating point.Its value range is unlimited. It is
recommended to use a float (instead of double) if you need to save memory in large arrays of floating point
numbers. The float data type should never be used for precise values, such as currency. Its default value is 0.0F.
Example: float f1 = 234.5f
Double Data Type
The double data type is a double-precision 64-bit IEEE 754 floating point. Its value range is unlimited. The double
data type is generally used for decimal values just like float. The double data type also should never be used for
precise values, such as currency. Its default value is 0.0d.
Example: double d1 = 12.3
Char Data Type
The char data type is a single 16-bit Unicode character. Its value-range lies between '\u0000' (or 0) to '\uffff' (or
65,535 inclusive).The char data type is used to store characters.
Example: char letterA = 'A'
Casting
It is a process of converting from one data type to another data type.casting of data types in java is also known as
type casting .There are Two Tyes of casting.
Implicit Casting(Widening Casting)
Widening Casting (automatically) - converting a smaller type to a larger type size
byte -> short -> char -> int -> long -> float -> double
Narrowing Casting (manually) - converting a larger type to a smaller size type
double -> float -> long -> int -> char -> short -> byte
Widening Casting
Widening casting is done automatically when passing a smaller size type to a larger size type:
Example
public class MyClass
{
public static void main(String[] args)
{
int myInt = 9;
double myDouble = myInt; // Automatic casting: int to double
System.out.println(myInt); // Outputs 9
System.out.println(myDouble); // Outputs 9.0 } }
Explicit Casting(Narrowing Casting, Explicit Casting)
It is also called as Narrowing casting
Narrowing casting must be done manually by placing the type in parentheses in front of the value:
Example
public class MyClass
{
public static void main(String[] args)
{
double myDouble = 9.78;
int myInt = (int) myDouble; // Manual casting: double to int
System.out.println(myDouble); // Outputs 9.78
System.out.println(myInt); // Outputs 9
}
}
Operators
Operators is a symbol that represent some operations that can be performed on data.It takes one or more
arguments that operates on them to produce a result .The constants, variables or expression on which operator
Operates are called as operands
For example:- 4+7 here + is the operator and 4 and 7 are operands
here are many types of operators in Java which are given below:
Arithmetic Operator,
Relational Operator,
Logical Operator,
Assignment Operator,
Increment and Decrement Operator,
Conditional Operator,
Bitwise Operator,
Special Operator.
Arithmetic Operators
Arithmetic operators are used to perform common mathematical operations.
Assume integer variable A holds 10 and variable B holds 20, then −
Show Examples
Operator Description Example
Adds values on either
+ (Addition) A + B will give 30
side of the operator.
Subtracts right-hand
- (Subtraction) operand from left-hand A - B will give -10
operand.
Multiplies values on either
* (Multiplication) A * B will give 200
side of the operator.
Divides left-hand
/ (Division) operand by right-hand B / A will give 2
operand.
Divides left-hand
operand by right-hand
% (Modulus) B % A will give 0
operand and returns
remainder.
The Relational Operators
There are following relational operators supported by Java language.Assume variable A holds 10 and variable B
holds 20, then −
Operator Description Example
Checks if the values of two operands are equal or not, if yes then
== (equal to) (A == B) is not true.
condition becomes true.
Checks if the values of two operands are equal or not, if values are not
!= (not equal to) (A != B) is true.
equal then condition becomes true.
Checks if the value of left operand is greater than the value of right
> (greater than) (A > B) is not true.
operand, if yes then condition becomes true.
Checks if the value of left operand is less than the value of right
< (less than) (A < B) is true.
operand, if yes then condition becomes true.
Checks if the value of left operand is greater than or equal to the value
>= (greater than or equal to) (A >= B) is not true.
of right operand, if yes then condition becomes true.
Checks if the value of left operand is less than or equal to the value of
<= (less than or equal to) (A <= B) is true
right operand, if yes then condition becomes true.
The Logical Operators
The following table lists the logical operators −
Assume Boolean variables A holds true and variable B holds false, then −
Show Examples
Operator Description Example
Called Logical AND
operator. If both the
&& (logical and) operands are non-zero, (A && B) is false
then the condition
becomes true.
Called Logical OR
Operator. If any of the
|| (logical or) two operands are non- (A || B) is true
zero, then the condition
becomes true.
Called Logical NOT
Operator. Use to reverses
the logical state of its
! (logical not) !(A && B) is true
operand. If a condition is
true then Logical NOT
operator will make false.
The Assignment Operators
Assignment operator is used to assign values to a variable. It is denoted by
the symbol”=“
Examples
OPERATOR DESCRIPTION EXAMPLE
Simple assignment operator. Assigns values from
= C = A + B will assign value of A + B into C
right side operands to left side operand.
Add AND assignment operator. It adds right
+= operand to the left operand and assign the result C += A is equivalent to C = C + A
to left operand.
Subtract AND assignment operator. It subtracts
-= right operand from the left operand and assign C -= A is equivalent to C = C – A
the result to left operand.
Multiply AND assignment operator. It multiplies
*= right operand with the left operand and assign the C *= A is equivalent to C = C * A
result to left operand.
Divide AND assignment operator. It divides left
/= operand with the right operand and assign the C /= A is equivalent to C = C / A
result to left operand.
Modulus AND assignment operator. It takes
%= modulus using two operands and assign the result C %= A is equivalent to C = C % A
to left operand.
Increment and Decrement Operator in Java :
1) It is one of the variation of “Arithmetic Operator“.
2) Increment and Decrement Operators are Unary Operators.
3)Unary Operator Operates on One Operand.
4) Increment Operator is Used to Increment Value Stored inside Variable on which it is operating.
5) Decrement Operator is used to decrement value of Variable by 1 (default).
Types of Increment and Decrement Operator :
Pre Increment / Pre Decrement Operator
Post Increment / Post Decrement Operator
Syntax :
++ Increment operator : increments a value by 1
-- Decrement operator : decrements a value by 1
Conditional operator:-( ? : )
This operator is also knows as ternary operator. It consists of three operands and is used to evaluate boolean
expressions . The general syntax of conditional operator is as follows :
variable x = (expression) ? value if true : value if false
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:
Primitive data types: The primitive data types include boolean, char, byte, short, int, long, float and double.
Non-primitive data types: The non-primitive data types include Classes, Interfaces, and Arrays.
Java Primitive Data Types
In Java language, primitive data types are the building blocks of data manipulation. These are the most basic data
types available in Java language
There are 8 types of primitive data types:
boolean data type
byte data type
char data type
short data type
int data type
long data type
float data type
double data type
Data Type Default Value Default size
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
Boolean Data Type
The Boolean data type is used to store only two possible values: true and false. This data type is used for
simple flags that track true/false conditions.
The Boolean data type specifies one bit of information, but its "size" can't be defined precisely.
Example: Boolean one = false
Byte Data Type
The byte data type is an example of primitive data type. It isan 8-bit signed two's complement integer. Its value-
range lies between -128 to 127 (inclusive). Its minimum value is -128 and maximum value is 127. Its default value
is 0.
The byte data type is used to save memory in large arrays where the memory savings is most required. It saves
space because a byte is 4 times smaller than an integer. It can also be used in place of "int" data type.
Example: byte a = 10, byte b = -20
Short Data Type
The short data type is a 16-bit signed two's complement integer. Its value-range lies between -32,768 to 32,767
(inclusive). Its minimum value is -32,768 and maximum value is 32,767. Its default value is 0.
The short data type can also be used to save memory just like byte data type. A short data type is 2 times smaller
than an integer.
Example: short s = 10000, short r = -5000
Int Data Type
The int data type is a 32-bit signed two's complement integer. Its value-range lies between - 2,147,483,648 (-2^31)
to 2,147,483,647 (2^31 -1) (inclusive). Its minimum value is - 2,147,483,648and maximum value is 2,147,483,647.
Its default value is 0.
The int data type is generally used as a default data type for integral values unless if there is no problem about
memory.
Example: int a = 100000, int b = -200000
Long Data Type
The long data type is a 64-bit two's complement integer. Its value-range lies between -9,223,372,036,854,775,808(-
2^63) to 9,223,372,036,854,775,807(2^63 -1)(inclusive). Its minimum value is - 9,223,372,036,854,775,808and
maximum value is 9,223,372,036,854,775,807. Its default value is 0. The long data type is used when you need a
range of values more than those provided by int.
Example: long a = 100000L, long b = -200000L
Float Data Type
The float data type is a single-precision 32-bit IEEE 754 floating point.Its value range is unlimited. It is
recommended to use a float (instead of double) if you need to save memory in large arrays of floating point
numbers. The float data type should never be used for precise values, such as currency. Its default value is 0.0F.
Example: float f1 = 234.5f
Double Data Type
The double data type is a double-precision 64-bit IEEE 754 floating point. Its value range is unlimited. The double
data type is generally used for decimal values just like float. The double data type also should never be used for
precise values, such as currency. Its default value is 0.0d.
Example: double d1 = 12.3
Char Data Type
The char data type is a single 16-bit Unicode character. Its value-range lies between '\u0000' (or 0) to '\uffff' (or
65,535 inclusive).The char data type is used to store characters.
Example: char letterA = 'A'
Casting
It is a process of converting from one data type to another data type.casting of data types in java is also known as
type casting .There are Two Tyes of casting.
Implicit Casting(Widening Casting)
Widening Casting (automatically) - converting a smaller type to a larger type size
byte -> short -> char -> int -> long -> float -> double
Narrowing Casting (manually) - converting a larger type to a smaller size type
double -> float -> long -> int -> char -> short -> byte
Widening Casting
Widening casting is done automatically when passing a smaller size type to a larger size type:
Example
public class MyClass
{
public static void main(String[] args)
{
int myInt = 9;
double myDouble = myInt; // Automatic casting: int to double
System.out.println(myInt); // Outputs 9
System.out.println(myDouble); // Outputs 9.0 } }
Explicit Casting(Narrowing Casting, Explicit Casting)
It is also called as Narrowing casting
Narrowing casting must be done manually by placing the type in parentheses in front of the value:
Example
public class MyClass
{
public static void main(String[] args)
{
double myDouble = 9.78;
int myInt = (int) myDouble; // Manual casting: double to int
System.out.println(myDouble); // Outputs 9.78
System.out.println(myInt); // Outputs 9
}
}
Operators
Operators is a symbol that represent some operations that can be performed on data.It takes one or more
arguments that operates on them to produce a result .The constants, variables or expression on which operator
Operates are called as operands
For example:- 4+7 here + is the operator and 4 and 7 are operands
here are many types of operators in Java which are given below:
Arithmetic Operator,
Relational Operator,
Logical Operator,
Assignment Operator,
Increment and Decrement Operator,
Conditional Operator,
Bitwise Operator,
Special Operator.
Arithmetic Operators
Arithmetic operators are used to perform common mathematical operations.
Assume integer variable A holds 10 and variable B holds 20, then −
Show Examples
Operator Description Example
Adds values on either
+ (Addition) A + B will give 30
side of the operator.
Subtracts right-hand
- (Subtraction) operand from left-hand A - B will give -10
operand.
Multiplies values on either
* (Multiplication) A * B will give 200
side of the operator.
Divides left-hand
/ (Division) operand by right-hand B / A will give 2
operand.
Divides left-hand
operand by right-hand
% (Modulus) B % A will give 0
operand and returns
remainder.
The Relational Operators
There are following relational operators supported by Java language.Assume variable A holds 10 and variable B
holds 20, then −
Operator Description Example
Checks if the values of two operands are equal or not, if yes then
== (equal to) (A == B) is not true.
condition becomes true.
Checks if the values of two operands are equal or not, if values are not
!= (not equal to) (A != B) is true.
equal then condition becomes true.
Checks if the value of left operand is greater than the value of right
> (greater than) (A > B) is not true.
operand, if yes then condition becomes true.
Checks if the value of left operand is less than the value of right
< (less than) (A < B) is true.
operand, if yes then condition becomes true.
Checks if the value of left operand is greater than or equal to the value
>= (greater than or equal to) (A >= B) is not true.
of right operand, if yes then condition becomes true.
Checks if the value of left operand is less than or equal to the value of
<= (less than or equal to) (A <= B) is true
right operand, if yes then condition becomes true.
The Logical Operators
The following table lists the logical operators −
Assume Boolean variables A holds true and variable B holds false, then −
Show Examples
Operator Description Example
Called Logical AND
operator. If both the
&& (logical and) operands are non-zero, (A && B) is false
then the condition
becomes true.
Called Logical OR
Operator. If any of the
|| (logical or) two operands are non- (A || B) is true
zero, then the condition
becomes true.
Called Logical NOT
Operator. Use to reverses
the logical state of its
! (logical not) !(A && B) is true
operand. If a condition is
true then Logical NOT
operator will make false.
The Assignment Operators
Assignment operator is used to assign values to a variable. It is denoted by
the symbol”=“
Examples
OPERATOR DESCRIPTION EXAMPLE
Simple assignment operator. Assigns values from
= C = A + B will assign value of A + B into C
right side operands to left side operand.
Add AND assignment operator. It adds right
+= operand to the left operand and assign the result C += A is equivalent to C = C + A
to left operand.
Subtract AND assignment operator. It subtracts
-= right operand from the left operand and assign C -= A is equivalent to C = C – A
the result to left operand.
Multiply AND assignment operator. It multiplies
*= right operand with the left operand and assign the C *= A is equivalent to C = C * A
result to left operand.
Divide AND assignment operator. It divides left
/= operand with the right operand and assign the C /= A is equivalent to C = C / A
result to left operand.
Modulus AND assignment operator. It takes
%= modulus using two operands and assign the result C %= A is equivalent to C = C % A
to left operand.
Increment and Decrement Operator in Java :
1) It is one of the variation of “Arithmetic Operator“.
2) Increment and Decrement Operators are Unary Operators.
3)Unary Operator Operates on One Operand.
4) Increment Operator is Used to Increment Value Stored inside Variable on which it is operating.
5) Decrement Operator is used to decrement value of Variable by 1 (default).
Types of Increment and Decrement Operator :
Pre Increment / Pre Decrement Operator
Post Increment / Post Decrement Operator
Syntax :
++ Increment operator : increments a value by 1
-- Decrement operator : decrements a value by 1
Conditional operator:-( ? : )
This operator is also knows as ternary operator. It consists of three operands and is used to evaluate boolean expressions . The
general syntax of conditional operator is as follows :
variable x = (expression) ? value if true : value if false
Bitwise Operator
Java defines several bitwise operators, which can be applied to the integer types, long, int, short, char, and byte.
Bitwise operator works on bits and performs the bit-by-bit operation. Assume if a = 60 and b = 13; now in binary
format they will be as follows −
a = 0011 1100
b = 0000 1101 -----------------
a&b = 0000 1100 a|b = 0011 1101
a^b = 0011 0001
~a = 1100 0011
The following table lists the bitwise operators −
Assume integer variable A holds 60 and variable B holds 13 then
Operator Description Example
Binary AND Operator copies a bit to the result if it
& (bitwise and) (A & B) will give 12 which is 0000 1100
exists in both operands.
Binary OR Operator copies a bit if it exists in
| (bitwise or) (A | B) will give 61 which is 0011 1101
either operand.
Binary XOR Operator copies the bit if it is set in
^ (bitwise XOR) (A ^ B) will give 49 which is 0011 0001
one operand but not both.
(~A ) will give -61 which is 1100 0011 in
Binary Ones Complement Operator is unary and
~ (bitwise compliment) 2's complement form due to a signed
has the effect of 'flipping' bits.
binary number.
Binary Left Shift Operator. The left operands
<< (left shift) value is moved left by the number of bits A << 2 will give 240 which is 1111 0000
specified by the right operand.
Binary Right Shift Operator. The left operands
>> (right shift) value is moved right by the number of bits A >> 2 will give 15 which is 1111
specified by the right operand.
Shift right zero fill operator. The left operands
value is moved right by the number of bits
>>> (zero fill right shift)
specified by the right operand and shifted A >>>2 will give 15 which is 0000 1111
values are filled up with zeros.
Special Operators
Java support some special operators 1:-Instance operators 2:-Dot Operators it is also known as member of selection
operators(.)
1:-Instance Operators :- The instance operators is an object reference operator and returns true or false result.
2:-Dot Operators:-The dot operators(.) is used to access the instance variable and method of class object.
Compiling and running java program
Step 1:
Write a program on the notepad and save it with .java (for example, DemoFile.java) extension.
class DemoFile
{
public static void main(String args[])
{
System.out.println("Hello!");
System.out.println("Java");
}
}
Step 2:
Open Command Prompt.
Step 3:
Set the directory in which the .java file is saved. In our case, the .java file is saved in C:\\demo.
Step 4:
Use the following command to compile the Java program. It generates a .class file in the same folder. It also shows an error if
any.
javac DemoFile.java
Step 5:
Use the following command to run the Java program:
java DemoFile
Command Line Arguments
The java command-line argument is an argument i.e. passed at the time of running the java program.
The arguments passed from the console can be received in the java program and it can be used as an input.
So, it provides a convenient way to check the behavior of the program for the different values. You can pass N
(1,2,3 and so on) numbers of arguments from the command prompt.
Simple example of command-line argument in java
In this example, we are receiving only one argument and printing it. To run this java program, you must pass at
least one argument from the command prompt.
class CommandLineExample
{
public static void main(String args[])
{
System.out.println("Your first argument is: "+args[0]);
}
}
compile by > javac CommandLineExample.java
run by > java CommandLineExample sonoo
Output: Your first argument is: sonoo
Accepting input from console (Using Buffered,Reader class, Scanner)
In Java, there are three different ways for reading input from the user in the command line environment(console).
1.Using Buffered Reader Class
This is the Java classical method to take input, Introduced in JDK1.0. 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.
Program:
// Java program to demonstrate BufferedReader
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test
{
public static void main(String[] args) throws IOException
{
//Enter data using BufferReader
BufferedReader reader =
new BufferedReader(new InputStreamReader(System.in));
// Reading data using readLine
String name = reader.readLine();
// Printing the read line
System.out.println(name);
}
}
Input: Geek
Output:Geek
Note: To read other types, we use functions like Integer.parseInt(), Double.parseDouble(). To read multiple values, we use split().
2. Using 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.
// Java program to demonstrate working of Scanner in Java
import java.util.Scanner;
class GetInputFromUser
{
public static void main(String args[])
{
// Using Scanner for Getting Input from User
Scanner in = new Scanner(System.in);
String s = in.nextLine();
System.out.println("You entered string "+s);
int a = in.nextInt();
System.out.println("You entered integer "+a);
float b = in.nextFloat();
System.out.println("You entered float "+b);
}
}
Input:
Geeks forGeeks 12 3.4
Output:
You entered integer 12 Enter a float You entered float 3.4
3. Using 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()).
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);
}
}
Arrays
Normally, an array is a collection of similar type of elements which has contiguous memory location.
Java array is an object which contains elements of a similar data type. Additionally, The elements of an array are stored in a
contiguous memory location. It is a data structure where we store similar elements. We can store only a fixed set of elements in a
Java array.
Array in Java is index-based, the first element of the array is stored at the 0th index, 2nd element is stored on 1st index and so on.
Unlike C/C++, we can get the length of the array using the length member. In C/C++, we need to use the sizeof operator.
In Java, array is an object of a dynamically generated class. Java array inherits the Object class, and implements the Serializable as
well as Cloneable interfaces. We can store primitive values or objects in an array in Java. Like C/C++, we can also create single
dimentional or multidimentional arrays in Java.
Moreover, Java provides the feature of anonymous arrays which is not available in C/C++.
Types of Array in java
There are two types of array.
Single Dimensional Array(One dimensional array)
Multidimensional Array
One Dimensional Array:-A list of items can be given one variable name using only one subscript(index)
and such a variable is called a single-subscripted or one-dimensional(1-D) array.
In java 1-D arrays can be declared as:
Syntax
Datatype array_name[index];
Example:-
Int a[20];
char name[30];
Float per[30];
Creation of Arrays:-
Creation of an array consists of two types
1.Declaring an array
2.Allocating memory mention above
General syntax to declare a one dimension array is above
General syntax of allocating memory to arrays
The new operator is used to allocate memory to an array
Array_name=new type[size];
Example:-
marks=new int[5];//Size of an array is 5
It is possible to combine the two steps declaration and allocation into one as below
type array_name[]=new type[size];
Example:-
int marks[]=new int[5];
Initializing Arrays:
An element of an array must be given a value before it is used. With java compilers all variables including
Array elements are given default initial values.Example all number elements are initialized to 0.All arrays
Store the allocated size in a variable named length.We can obtain the length of array using
Array_name length.
We can initialize values to the array elements using assigments operator as
Array name[index]=value;
Example:-
marks[0]=50;
marks[1]=60;
Marks[4]=70;
We can also initialize arrays automatically in the same way as the ordinary variable
Type array_name[]={list of values};
Example:-
Int marks[]={50,60,70,90,80};
Example on one dimensional array
import java.io.*;
class Onedimension
{
public static void main(String args[])
{
int[] a=new int[3];//declaration
a[0]=10;//initialization
a[1]=20;
a[2]=30;
//printing array
System.out.println("One dimensional array elements are");
System.out.println(a[0]);
System.out.println(a[1]);
System.out.println(a[2]);
}
}
Two Dimensional Array(Multidimensional Array)
The Two Dimensional Array in Java programming language is nothing but an Array of Arrays. In Java Two
Dimensional Array, data stored in row and columns, and we can access the record using both the row index and
column index (like an Excel File).
The general syntax to create two dimensional array is as below:-
Datatype arrayname[][]=new datatype[row][column];
Example:-int table[][]=new[4][3];
This will create total 12 storage location for two dimensional array.
class Twodimension
{
public static void main(String args[])
{
int[][] a={{10,20},{30,40}};//declaration and initialization
System.out.println("Two dimensional array elements are");
System.out.println(a[0][0]);
System.out.println(a[0][1]);
System.out.println(a[1][0]);
System.out.println(a[1][1]);
}
}