Java Basics to Average Notes
Java Basics to Average Notes
▪ After Java 7, underscores can be added to number literals such as 1_000_000 (To denote 1
million).
1. Positive infinity.
2. Negative infinity.
▪ int m = 7;
▪ int n = 7;
▪ int a = 2 * ++m; // now a is 16, m is 8
▪ int b = 2 * n++; // now b is 14, n is 8
- objectname.method();
//PROGRAM
a=in.nextInt();
b=in.nextInt();
C=a<b?a:b; 5<10?5:10
System.out.println("C = " + c);
if-else Statement:
if(condition){
//code if condition is true
}else{
//code if condition is false
}
if(condition1){
//code to be executed if condition1 is true
}else if(condition2){
//code to be executed if condition2 is true
}
else if(condition3){
//code to be executed if condition3 is true
}
...
else{
//code to be executed if all the conditions are false
}
Dr.P.Dinesh Kumar, AP/Sri Eshwar College of Engineering
Control Statements (Contd.,)
Do While Loop:
▪ The array length need not be a constant: new int[n] creates an array of length n.
java compiler creates a default constructor if your class doesn’t have any.
• Constructors must have the same name as the class within which it is defined while it
• Constructors do not return any type while method(s) have the return type or void if
• Constructors are called only once at the time of Object creation while method(s) can
Break Statement:
Continue Statement:
- The continue statement is used in loop control structure when you need to jump to the
- It continues the current flow of the program and skips the remaining code at the
specified condition.
Two Ways:
• Sub Class/Child Class: Subclass is a class which inherits the other class. It is also
called a derived class, extended class, or child class.
• Super Class/Parent Class: Superclass is the class from where a subclass inherits the
features. It is also called a base class or a parent class.
▪ If A and B classes have the same method and you call it from child class
reference variable.
Dr.P.Dinesh Kumar, AP/Sri Eshwar College of Engineering
Super Keyword (Contd.,)
superclass.
▪ Both the superclass and the subclass must have the same method
name, the same return type and the same parameter list.
▪ Java uses the principle of ‘a superclass reference variable can refer to a subclass
object’ to resolve calls to overridden methods at run time.
implementation.
The final keyword in java is used to restrict the user. Final can be:
1.variable
2.method
3.class
• Abstract method: can only be used in an abstract class, and it does not
have a body. The body is provided by the subclass (inherited from).
Dr.P.Dinesh Kumar, AP/Sri Eshwar College of Engineering
Abstraction (Continued)
▪ An abstract class can have both abstract and regular methods:
Syntax:
datatype[ ][ ] arrayname = new datatype[rows][columns];
▪ Ragged Arrays.
➢Packages
▪ HTML tags can be used in the free-form of text like, <em> … </em>,
<strong>…</strong> etc.
Dr.P.Dinesh Kumar, AP/Sri Eshwar College of Engineering
Class Comments
▪ Placed after import statement
@return description
This tag adds a “returns” section to the current method
/**
* The "Hearts" card suit
*/
public static final int HEARTS = 1;
▪ Same I/O classes and methods can be applied to different types of devices.
▪ Input stream can abstract different kinds of input – from file, keyboard or network
socket.
Byte Streams:
Character Streams:
- Two most important are read() and write() – Read and Write
bytes of data.
▪ These fields are declared as public, static and final within the system.
▪ Can be used in any part of the program without creating a reference to the specific system
object.
1.write(int b)
Static method
• A static method is one that can be invoked without creating object of a class.
• It can access static variables without using the object of the class.
• Static class
• We can declare a class as static only if it is a nested class.
2 Categories:
- Built-in packages.
- Private
- Protected
- Public
• Any other class of the same package will not be able to access these
members.
keyword protected.
different packages.
Equality:
- s.equals(t) // To test two strings are equal or not (Returns Boolean)
- "Hello".equalsIgnoreCase("hello")
String Length:
▪ String greeting = "Hello";
▪ int n = greeting.length(); // is 5
replace:
trim:
Changing Cases:
Joining Strings:
- String.join(“ “, “Welcome”,”Java”);
Four Constructors:
- StringBuffer() – Reserves space for 16 Characters
- StringBuffer(int size)
- StringBuffer(String str) – str + Add 16 Char
- StringBuffer(CharSequences chars) – Chars + Add 16 Char
ensureCapacity:
- To be handled gracefully.
Exception Handling:
• The public access modifier has the widest scope among all other
access modifiers.
▪ To perform different tasks at the occurrence of different exceptions, use java multi-
catch block.
To Remember:
▪ At a time only one exception occurs and at a time only one catch block is executed.
▪ All catch blocks must be ordered from most specific to most general, i.e. catch for
ArithmeticException must come before catch for Exception.
Dr.P.Dinesh Kumar, AP/Sri Eshwar College of Engineering
Contd.,
}
catch(Exception e1)
{
//exception message
}
}
//catch block of parent (outer) try block
catch(Exception e3)
{
//exception message
}
States of a thread:
- Newborn
- Runnable
- Running
- Blocked
- Dead
MIN_PRIORITY = 1
NORM_PRIORITY = 5
MAX_PRIORITY = 10
Why Synchronization?
▪ Same I/O classes and methods can be applied to different types of devices.
▪ Input stream can abstract different kinds of input – from file, keyboard or network
socket.
Byte Streams:
Character Streams:
- Two most important are read() and write() – Read and Write
bytes of data.
▪ These fields are declared as public, static and final within the system.
▪ Can be used in any part of the program without creating a reference to the specific system
object.
BufferedReader class.
1.write(int b)
class.
toString() Method:
public String toString()
{
return “welcome”;
}
charAt();
String s=“Hai”;
System.out.println(s.charAt(1));
- int indexOf(int ch, int startIndex) // Specify a starting point for search
- int lastIndexOf(int ch, int startIndex) // Specify starting point to search from last to zero.
▪ The trim( ) method returns a copy of the invoking string from which any leading and trailing spaces
have been removed.
Changing Cases:
buffer = Hello
length = 5
capacity = 21
Its capacity is 21 because room for 16 additional characters is automatically added.
Dr.P.Dinesh Kumar, AP/Sri Eshwar College of Engineering
String Buffer (Contd.,)
ensureCapacity():
- To preallocate room for certain number of characters after a StringBuffer has been
constructed, you can use ensureCapacity() to set the size of buffer.
setLength():
- If you call setLength( ) with a value less than the current value returned by length( ),
then the characters stored beyond the new length will be lost.