Java Notes
Java Notes
Unboxing:
• Unboxing is the automatic conversion done by the java compiler where the object
of wrapper is converted to its corresponding primitive data type.
eg: int x=125;
Integer ob2=new Integer(x);
int y=ob2;//unboxing here the object ob2 value is directly assigned to
primitive type variable i.e y.
ARRAYS:-
An array is a collection of homogenous elements referred by the same name and is stored
in contiguous memory location. An array is a memory structure created that contains data
with similar data type.
The index number of the array used to access the array elements is said to be subscript
of the array.
Types of arrays:-
a) One Dimensional array:- Array that is indexed by a single subscript.
b) Double Dimensional array:- Array that are indexed by two subscripts-one for the rows
and the other for the columns.
Differentiate between the following:
Primitive data type Composite data type
Data types that are provided by java and Data type created by a programmer which
allows specific type of data to be stored by a makes use of primitive data type to create a
variable. variable as per the user’s requirement.
The size is fixed. The size depends on the number of member
variables and their data types.
switch if-else
This statement is a multiple branching It is bidirectional control flow statement
statement
It performs tests on integer as well as It performs tests on integer, float and
character point values. character values
break continue
It terminates the loop bypassing the It forces early iteration of loop where the
remaining code in the body of the loop. control goes back for the next iteration of
the loop, skipping the remaining code of
the body of the loop for that particular
iteration.
next( ) nextLine( )
It is used to input a word. It is used to input a sentence.
Delimiter used is whitespace Delimiter used is enter key
nextInt( ) hasNextInt( )
This method receives next token from This method returns true if the next token
scanner object which can be expressed as can be expressed as integer , otherwise
an integer and can be stored in a variable returns false.
of type int.
Return type is int. Return type is boolean
Token Identifier
It is the smallest individual unit in a Identifier is the name given to different
program. data items of the program like variables,
methods , objects etc
boolean literal character literal
Literals having any one of the values of Single character enclosed in single quotes
true or false
Reserves memory of one byte but uses Reserves 2 bytes of memory.
only 1 bit
Constructor Method
Constructors are used to initialize the state Methods are used to perform some
of an object. specific tasks.
The class name and constructor name The class name and the method name
should be same should be different
Constructor does not have a return type Methods should have a return type
Important points:-
1. Keyword used to allocate memory to any array or composite data type: new
2. Methods of scanner class:
i)to input an integer from the standard input stream:nextInt( )
ii) to input a String data from the standard input stream:next ( ) or nextLine( )
3. A package that is imported by default: java.lang
4. A key word to use the classes defined in a package: import
5. Package that contains the class System: java.lang
6. The package that contains the class Scanner: java.util
7. String function to remove blank spaces provided in the prefix and suffix
of a String: trim()
8. The method that converts a String to a primitive integer type.
parseInt( ) & valueOf( ) (Wrapper class methods)
9. Some library classes: String, Math, System etc
10.Access specifier that provides most accessibility is public and least accessibility is
private.
11.Keyword that indicates that a method has no return type: void
12.Keyword that causes the control to transfer back to the method call: return
13.Keyword that distinguishes between instance variables and class variables.
static
14.The OOP’s principle that implements function overloading: Polymorphism
15. Character set used in java : Unicode(16 bits)
16. Default value of a boolean datatype: false
17. Default value of char : ‘\u0000’
18. Keyword of java to define a variable as constant: final
19. To read a character using scanner class method is
char ch =sc.next().charAt(0);//sc is scanner class object.
20.System.exit(0); is a system call that exits current program by terminating running Java
Virtual Machine .This function takes argument as status code . Argument ‘0’ is the status
code which indicates normal termination of the program.
Input/output in java:
• A stream can be defined as a sequence of data. The InputStream is used to read
data from a source and the OutputStream is used for writing data to a destination.
• A source of input data is called InputStream and output data is called OutputStream
• The two types of stream are byte stream and character stream.
• Three predefined objects present in java.lang are
System.in, System.out, System.err
• System.in refers to an input stream managed by the System class that implements
the Standard InputStream. System.in is an InputStream object and it is connected to
the standard input device( keyboard).
• System.out refers to an output stream managed by the System class that
implements the standard OutputStream. It is the object of the OutputStream class
and connected to standard output device monitor, System.err stream is also
connected to monitor.
Scanner class:
• The Scanner class is latest development in java , which allows user to input the
values of various types. The Scanner class is defined in java.util package.
• Syntax to create a Scanner object is :Scanner sc=new Scanner(System.in);
• The scanner class basically works on the principle of tokens in the set of values input
from console.
• A token is a series of characters that ends with a delimiter. Some delimiters which
can be used as token separators are comma(,), fullstop(.),semicolon(;),whitespace
etc. Scanner class uses whitespace as default token separator. A white space
character can be a blank, a tab character, a carriage return, or end of the file.
Method Description
Scanner sc=new Scanner(System.in); Returns the next token as an int. If the
int n=sc.nextInt(); next token is not an integer,
InputMismatchException is thrown.
Scanner sc=new Scanner(System.in); Returns the next token as a long. If the
long n=sc.nextLong(); next token is not an integer,
InputMismatchException is thrown.
Scanner sc=new Scanner(System.in); Returns the next token as a float. If the
float n=sc.nextFloat(); next token is not a float,
InputMismatchException is thrown.
Scanner sc=new Scanner(System.in); Returns the next token as a double. If the
double n=sc.nextDouble(); next token is not a double,
InputMismatchException is thrown.
Scanner sc=new Scanner(System.in); Finds and returns the next complete
String n=sc.next(); token as a String(a token is usually ended
by a whitespace). If no token exists,
NoSuchElementException is thrown.
Scanner sc=new Scanner(System.in); Returns the rest of the current line.
String str=sc.nextLine();
boolean b=sc.hasNextLine(); Returns true if the scanner has another
line in its input, otherwise returns false.
boolean b=sc.hasNextFloat(); Returns true if the next token can be
interpreted as float value, otherwise
returns false.
boolean b=sc.hasNextInt(); Returns true if the next token can be
interpreted as int value, otherwise
returns false.
boolean b=sc.hasNext(); Returns true if the scanner has another
token in its input, otherwise returns
false.
System.out.println("Banana".compa
reToIgnoreCase("grapes"));
//-5
30 String Returns a int x=12;
toString(number) numerical string String s1=Integer.toString(x);
by converting float y=12.3f;
numbers to string. String s2=Float.toString(y);
double z=12.3;
String s3=Double.toString(z);
31 String Returns a int x=12;
valueOf(number) numerical string String s1=String.valueOf(x);
by converting float y=1.2f;
numbers to string String s2=String.valueOf(y);
double z=67.7;
String s3=String.valueOf(z);