Section II
Basics of Java Programing Language
Note:
• Java does not support any unsigned types.
• Variables of type char, byte, short, int, long, float and double are all
given the value “0” by default.
• Variable of type Boolean are given ‘false’ by default.
• All primitive datatypes in Java are portable.
• Non- Primitive Datatype:
• These are also known as
• Derived Datatype, or
• Abstract Datatype, or
• Reference Type.
• These are built-on primitive data types.
• Examples: strings, class, array, interface etc.,
Variables
• Variable is a name of memory location. Types of Variable
• There are three types of variables in java:
• local variable
• instance variable
• static variable
• Local Variable: A variable which is declared inside the method is called
local variable.
• Instance Variable: A variable which is declared inside the class but outside
the method, is called instance variable . It is not declared as static.
• Static variable: A variable that is declared as static is called static variable.
It cannot be local.
• Instance Variables:
• Scope: Throughout the class except in the static methods.
• Lifetime: Until the object of the class stays in the memory.
• Class/Static Variables:
• Scope: Throughout the class.
• Lifetime: Until the end of the program.
• Local Variables:
• Scope: Within the block it is declared.
• Lifetime: Until control leaves the block in which it is declared.
Strings: Methods
• 1. compareToIgnoreCase()
public class Main {
public static void main(String[] args) {
String myStr1 = "HELLO";
String myStr2 = "hello";
[Link]([Link](myStr2));
}
}
• 2. compareTo()
public class Main {
public static void main(String[] args) {
String myStr1 = "Hello";
String myStr2 = "Hello";
[Link]([Link](myStr2)); // Returns 0 because they are equal
}
}
• trim()
public class Main {
public static void main(String[] args) {
String myStr = " Hello World! ";
[Link](myStr);
[Link]([Link]());
}
}
• substring()
public class Main {
public static void main(String[] args) {
String myStr = "Hello, World!";
[Link]([Link](7, 12));
}
}
• indexOf()
public class Main {
public static void main(String[] args) {
String myStr = "Hello planet earth, you are a great planet.";
[Link]([Link]("planet"));
}
}
• lastIndexOf()
public class Main {
public static void main(String[] args) {
String myStr = "Hello planet earth, you are a great planet.";
[Link]([Link]("planet"));
}
}
• replace()
public class Main {
public static void main(String[] args) {
String myStr = "Hello";
[Link]([Link]('l', 'p'));
}
}
• subSequence()
public class Main {
public static void main(String[] args) {
String myStr = "Hello, World!";
[Link]([Link](7, 12));
}
}