0% found this document useful (0 votes)
7 views141 pages

Oops With Java (Autosaved)

The document provides an overview of the basics of Java programming, including details on primitive and non-primitive data types, as well as the different types of variables: local, instance, and static. It also explains the scope and lifetime of these variables and includes examples of string methods such as compareToIgnoreCase, trim, substring, indexOf, lastIndexOf, replace, and subSequence. Overall, it serves as a foundational guide for understanding Java programming concepts and string manipulation.

Uploaded by

Rishav Dev
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views141 pages

Oops With Java (Autosaved)

The document provides an overview of the basics of Java programming, including details on primitive and non-primitive data types, as well as the different types of variables: local, instance, and static. It also explains the scope and lifetime of these variables and includes examples of string methods such as compareToIgnoreCase, trim, substring, indexOf, lastIndexOf, replace, and subSequence. Overall, it serves as a foundational guide for understanding Java programming concepts and string manipulation.

Uploaded by

Rishav Dev
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 141

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";
System.out.println(myStr1.compareToIgnoreCase(myStr2));
}
}
• 2. compareTo()
public class Main {
public static void main(String[] args) {
String myStr1 = "Hello";
String myStr2 = "Hello";
System.out.println(myStr1.compareTo(myStr2)); // Returns 0 because they are equal
}
}
• trim()
public class Main {
public static void main(String[] args) {
String myStr = " Hello World! ";
System.out.println(myStr);
System.out.println(myStr.trim());
}
}

• substring()
public class Main {
public static void main(String[] args) {
String myStr = "Hello, World!";
System.out.println(myStr.substring(7, 12));
}
}
• indexOf()
public class Main {
public static void main(String[] args) {
String myStr = "Hello planet earth, you are a great planet.";
System.out.println(myStr.indexOf("planet"));
}
}
• lastIndexOf()
public class Main {
public static void main(String[] args) {
String myStr = "Hello planet earth, you are a great planet.";
System.out.println(myStr.lastIndexOf("planet"));
}
}
• replace()
public class Main {
public static void main(String[] args) {
String myStr = "Hello";
System.out.println(myStr.replace('l', 'p'));
}
}
• subSequence()
public class Main {
public static void main(String[] args) {
String myStr = "Hello, World!";
System.out.println(myStr.subSequence(7, 12));
}
}

You might also like