Core Java(Part-5)

Content
1.Methods
.Predefined methods
.Userdefined methods
.Method without parameter without return type
.Method with parameter without return type
.Method without parameter with return type
.Method with parameter with return type
2.String
Methods
Types in Method
predefined method
eg:- System.out.println()
, Math.sqrt()
, etc.
User defined methods
eg:- sayHello()
, addNumbers()
In java methods is of 4 types
1. Method without parameter without return type
use when you didn’t return anything
Syntax:-
public void hello(){
int a=12;
System.out.println(a);
}
2. Method with return type without parameter
Syntax:-
public int hello1(){
int a=12;
int b=10;
int c=a+b;
return c;
}
3. Method without return type with parameter
Syntax:-
public class MethodWithParameterWithoutReturnType {
public static void main(String args[]){
hello1(10);
}
public static void hello1(int a) {
int a1 = a;
System.out.println(a1);
}
}
4. Method with parameter with return type
Syntax:-
public class MethodWithParameterWithReturnType {
public static void main(String args[]){
System.out.println(hello1(10,20));
}
public static int hello1(int a, int b) {
int a1 = a;
int b1=b;
return a1+b1;
}
}
In lame term
Instead of cooking every time from scratch, you press a button on your sandwich maker 🥪 That button is like a method — it does a specific job when you call it.
void makeSandwich() {
System.out.println("Bread + Sauce + Cheese = Sandwich ");
}
Now wherever you say
makeSandwich();
Java be like:
“On it, boss! Sandwich aa gaya”
void | Kuch return nahi karega |
int , String , etc. | Ye batata hai ki method kya return karega |
() | Method ke inputs (parameters) |
{} | Method ke andar ka code |
String
String in simple words sentence, words, letter of collection.
In java when you have to handle something like someone name, message or sentence there we use String.
String name=”Khelendra”;
System.out.println(name);
String text enclosed in double quotes(““)
String is a class in java, but to use String is very easy like using other datatype (int, float) etc.
String are immutable means when you create String variable and store value in it after initialization you cannot reinitialized it(you can’t change the value again)
Common String function
- length()
It tells how many character in String
String str=”Khelendra”
System.out.println(str.length());//9
2.toUpperCase() / toLowerCase()
toUpperCase() It convert the String into capital letter(upper case)
toLowerCase() It convert the String into small letter(lower case)
str.toUpperCase(); // "KHELENDRA"
str.toLowerCase(); // "khelendra"
3.charAt(index)
It tells in which position which letter are at
str.charAt(2);//
4.equals() vs ==
equals()
it check actual content inside the variable
==
it check memory allocation(reference address)
public class StringComparision{
public static void main(String args[]){
String s1="Hello";
String s2="Hello";
System.out.println(s1==s2);//true
System.out.println(s1.equals(s2));//true
String s3=new String("Hello");
System.out.println(s1==s3);//false
System.out.println(s1.equals(s3));//true
}
}
Whenever you use new keyword then it create two object first object is create in heap and second object is create in string constant pool(scp)
Whenever you create String then it goes to two area first in heap or second in String constant pool
String Constant Pool(SCP)
. Java create a special place for String in SCP
. When you try to create a one more same String then java didn’t create a new duplicate String of that old String in place of that it gives you the old one
. It saves the memory
Heap
When you use new String()
then java forcefully create new object whether in it the data is same.
Code | Where it goes | Same object? |
String a = "hi"; | SCP (special shelf) | Yes, if reused |
String b = "hi"; | SCP | Same as a |
String c = new String("hi"); | Heap (messy room) | Different from a |
StringBuffer
It is mutable, also thread safe
public class StringBufferDemo{
public static void main(String args[]){
StringBuffer sb=new StringBuffer(“Khelendra“);
sb.append(“Thakur“);
System.out.println(sb);
}
}
String and StringBuffer
public class StringAndStringBuffer{
public static void main(String args[]){
String s1=”hello”;
String s2=s1.concat(“world“);
System.out.println(s1);//hello
System.out.println(s2);//helloworld (new object created)
StringBuffer sb=new StringBuffer(“raj“);
StringBuffer sb1=sb.append(“yadav“);
System.out.println(sb);//raj
System.out.println(sb==sb1);//true
}
}
StringBuilder
It is mutable, not thread safe.
public class StringAndStringBuffer{
public static void main(String args[]){
String s1=”hello”;
String s2=s1.concat(“world“);
System.out.println(s1);//hello
System.out.println(s2);//helloworld (new object created)
StringBuilder sb=new StringBuilder(“raj“);
StringBuilder sb1=sb.append(“yadav“);
System.out.println(sb);//raj
System.out.println(sb==sb1);//true
}
}
Subscribe to my newsletter
Read articles from Khelendra Kumar Thakur directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
