college.staff.cse.Employee
and college.staff.ee.Employee
com.company.project
).com.company.project
, create a directory structure com/company/project/
.com/company/project/
), place your Java source files (.java files).com.company.project
:package com.company.project;
public class MyClass {
// Class code here
}
javac com/company/project/MyClass.java
Using packages in Java involves importing classes from other packages into your Java source files so that you can utilize their functionality. Here’s a step-by-step guide on how to use packages in Java:
import
statement at the beginning of your Java file.import packageName.ClassName;
import packageName.*;
import static packageName.ClassName.staticMember;
Suppose you have a package com.company.project
with a class MyClass
, and you want to use it in another class Main:
MyClass.java
: Define MyClass
inside com.company.project
package com.company.project;
public class MyClass {
public void display() {
System.out.println("Hello from MyClass");
}
}
Main.java
: Import MyClass
and use it in Main// Import MyClass from com.company.project package
import com.company.project.MyClass;
public class Main {
public static void main(String[] args) {
MyClass obj = new MyClass(); // Create an instance of MyClass
obj.display(); // Call display() method from MyClass
}
}
MyClass.java
and Main.java
(from the root directory containing com/
)javac com/company/project/MyClass.java
javac Main.java
java Main
Hello from MyClass
[!Important]
- Package Naming: Ensure package names follow Java's naming conventions (
com.company.project
).- Import Statements: Always place
import
statements at the beginning of your Java file, before any class definitions.- Class Usage: Once imported, classes from other packages can be instantiated and their methods invoked like any other class in your project
String demoString = “Ankit”;
String demoString = new String (“GeeksforGeeks”);
int length()
"Ankit".length(); // returns 5
Char charAt(int i)
"Ankit".charAt(3); // returns ‘i’
String substring(int i)
"Helloworld".substring(3); // returns “oworld”
String substring(int i, int j)
"Helloworld".substring(2,5); // returns “llo”
String concat(String str)
String s1 = ”hello”;
String s2 = ”world";
String output = s1.concat(s2); // returns “helloworld”
String toLowerCase()
case. String word1 = “HeLLo”;
String word3 = word1.toLowerCase(); // returns “hello"
String toUpperCase()
String word1 = “HeLLo”;
String word2 = word1.toUpperCase(); // returns “HELLO”
String trim()
String word1 = “ Learn Share Learn “;
String word2 = word1.trim(); // returns “Learn Share Learn”
String replace(Char old, Char new)
oldChar
with newChar
.String s1 = “feeksforfeeks“;
String s2 = “feeksforfeeks”.replace(‘f’ ,’g’); // returns “geeksgorgeeks”
StringBuffer is a class in Java that represents a mutable sequence of characters.
It provides an alternative to the immutable String class, allowing you to modify the contents of a string without creating a new object every time
append()
Method:
append()
method concatenates the given argument with this string.import java.io.*;
class A {
public static void main(String args[]) {
StringBuffer sb = new StringBuffer("Hello ");
sb.append("Java"); // now original string is changed
System.out.println(sb);
}
}
// OUTPUT
// Hello Java
insert()
Method:
insert()
method inserts the given string with this string at the given position.import java.io.*;
class A {
public static void main(String args[]) {
StringBuffer sb = new StringBuffer("Hello ");
sb.insert(1, "Java"); // Now original string is changed
System.out.println(sb);
}
}
// OUTPUT
// HJavaello
replace()
Method:
replace()
method replaces the given string from the specified beginIndex
and endIndex-1
.import java.io.*;
class A {
public static void main(String args[]) {
StringBuffer sb = new StringBuffer("Hello");
sb.replace(1, 3, "Java");
System.out.println(sb);
}
}
// OUTPUT
// HJavalo
delete()
Method:
delete()
method of the StringBuffer class deletes the string from the specified beginIndex
to endIndex-1
.import java.io.*;
class A {
public static void main(String args[]) {
StringBuffer sb = new StringBuffer("Hello");
sb.delete(1, 3);
System.out.println(sb);
}
}
// OUTPUT
// Hlo
ensureCapacity()
method.append()
method is used to add characters, strings, or other objects to the end of the buffer.insert()
method is used to insert characters, strings, or other objects at a specified position in the buffer.delete()
method is used to remove characters from the buffer.reverse()
method is used to reverse the order of the characters in the buffer.String | StringBuffer |
---|---|
String is immutable. | It is mutable. |
It is slow in terms of executing the concatenation task. | It is fast in terms of executing the concatenation task. |
Here the length of the string class is static. | Here the length can be modified whenever required, as it is dynamic in behaviour. |
It is less efficient | It is more efficient in nature as compared to the string class. |
String consumes more as compared to the StringBuffer. | StringBuffer uses less memory as compared to the string. |
It utilizes a string constant pool to store the values. | It prefers heap memory to store the objects |
It overrides both equal() and hashcode() techniques of object class. | It cannot override equal() and hashcode() methods. |
PrintStackTrace()
: This method prints exception information in the format of the Name of the exception: description of the exception, stack trace.//program to print the exception information using printStackTrace() method
import java.io.*;
class GFG {
public static void main (String[] args) {
int a=5;
int b=0;
try {
System.out.println(a/b);
}
catch (ArithmeticException e) {
e.printStackTrace();
}
}
}
// OUTPUT
// java.lang.ArithmeticException: / by zero
// at GFG.main(GFG.java:10)
toString()
: The toString()
method prints exception information in the format of the Name of the exception: description of the exception.//program to print the exception information using toString() method
import java.io.*;
class GFG1 {
public static void main (String[] args) {
int a=5;
int b=0;
try {
System.out.println(a/b);
}
catch(ArithmeticException e) {
System.out.println(e.toString());
}
}
}
// OUTPUT
// java.lang.ArithmeticException: / by zero
getMessage()
: The getMessage()
method prints only the description of the exception.//program to print the exception information using getMessage() method
import java.io.*;
class GFG1 {
public static void main (String[] args) {
int a=5;
int b=0;
try {
System.out.println(a/b);
}
catch(ArithmeticException e) {
System.out.println(e.getMessage());
}
}
}
// OUTPUT
// / by zero
Made By SOU Student for SOU Students