Notes
Notes
Topics To Cover:
Working with String.
1. Converting String to character array
2. Converting String to Byte Array
3. Discussion of OCJP Questions based on String.
4. Todo: Watch videos on String on Tech Code Guruji [CwB]
5. Completion of SplashFrame in Swing.
6. Doubt Clearance.
-----------------------------------------------------------------------------------
--------------
Working with String.
1. Converting String to character array
Example Program: Counting number of 'a' in the given String [Interview]
import java.util.Scanner;
public class CountWordExample{
public static void main(String args[ ]){
Scanner s=new Scanner(System.in);
System.out.print("Enter a String ");
String n=s.nextLine(); //It takes String with Spaces
//Now, convert String n to character array
char c[ ]=n.toCharArray();
//Perform a loop from index 0 to length-1 and count number of 'a'
int count=0;
for(int i=0; i<c.length; i++){
if(c[i]=='a'){ //Ask i-th letter it's a or not
count++; //if yes then increase counter by 1
}
}
//Display Value of Count
System.out.printf("Total Number of a in %s is %d" , n , count);
} //main
} //class
import java.util.Scanner;
public class CountWordExample{
public static void main(String args[ ]){
Scanner s=new Scanner(System.in);
System.out.print("Enter a String ");
String n=s.nextLine(); //It takes String with Spaces
//Now, convert String n to character array
char c[ ]=n.toCharArray();
//Perform a loop from index 0 to length-1 and count number of 'a'
int count=0;
for(int i=0; i<c.length; i++){
if(c[i]=='a'){ //Ask i-th letter it's a or not
count++; //if yes then increase counter by 1
}
}
//Display Value of Count
System.out.printf("Total Number of a in %s is %d" , n , count);
} //main
} //class
import java.util.Scanner;
public class ByteArrayExample{
public static void main(String args[ ]){
Scanner s=new Scanner(System.in);
System.out.print("Enter a String ");
String n=s.nextLine(); //It takes String with Spaces
//Now, convert String n to byte array
byte c[ ]=n.getBytes();
//Perform a loop from index 0 to length-1 and count number of 'a'
int count=0;
for(int i=0; i<c.length; i++){
System.out.print(c[i] + "\t"); // \t - tab - gives six spaces (*1)
}
} //main
} //class
Now, in place of (*1) - We can display ASCII Codes in form of Binary String by
using Wrapper classes.
(*1) => System.out.print(Integer.toBinaryString(c[i]) + "\t");
OCJP4. Try at Home [Inside PSVM] - What is the default capacity of StringBuffer
StringBuffer sb=new StringBuffer();
System.out.println(sb.capacity());
OCJP5. How we can find out number of characters in a given String variable?
(a) length (b) size (c) length() (d) size()
OCJP6. How we can find out number of items in a given array variable?
(a) length (b) size (c) length() (d) size()
OCJP8. Google Search: Why String object can be created with and without new
keyword in Java?
Ans:
In Java (Python/C#/Apple swift), special treatment is given to String class. It can
be used with and without new
a) Without new=> It will get memory from SCP - String Constant Pool
b) With new =>It will get memory from Heap
Example:
OCJP1: Most important Question
String a="india";
String b="india";
String c=new String("india");
String d=new String("india");
How many reference variables and
objects are created in above
code snippet?
OCJP2: Try it inside psvm, note down result [Most Imp Concept]
String a="india";
String b="india";
String c=new String("india");
if(a==b)
S.o.p("a==b");
if(a==c)
S.o.p("a==c");
if(a.equals(b))
S.o.p("a eq b");
if(a.equals(c))
S.o.p("a eq c");
Swing=>Completing SplashFrame
Covered on 17-Sep-2020
Step I: File=>New File=>Category: Swing GUI Type=>JFrame
Step II: Design Layout using Labels with JProgressBar=> name=>jProgressBar1
on 19-Sep-2020
Step III: Click on [Source] after public class SplashFrame extends JFrame
add
implements ActionListener
Step VIII: After ending } of above code, define a function to move progress bar as
public void actionPerformed(ActionEvent evt){
//Obtain current value of progress bar
int v=jProgressBar1.getValue();
//Increase value by 1
v++;
//Check if reaches to 101
if(v==101){
t.stop(); //Stop the Timer
//Leave a Line for Future use
this.dispose(); //Close this Frame
return; //Terminate Fn
}
//Otherwise, update value of jProgressBar
jProgressBar1.setValue(v);
}