Chapter 2 Basic of Java New
Chapter 2 Basic of Java New
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
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
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
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
Example
public class MyClass
{
public static void main(String[] args)
{
double myDouble = 9.78;
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.
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
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 :
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
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.
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
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
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));
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++.
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]);
}
}