You are on page 1of 4

Lecture 19-Sep-2020 Saturday

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");

Similarly, we can use toHexString(c[i]) and toOctalString(c[i]);

CQ: What is the need of converting String to byte array?


Ans: converting to byte (0/1) is required for transmitting data over network.
[TCP/OSI Model]

3. Discussion of OCJP Questions based on String.


OCJP3. Try at Home [Inside PSVM]
StringBuffer sb=new StringBuffer();
sb.append("TecDev");
System.out.println(sb.length()); //Output: 6
System.out.println(sb.capacity()); //Output: 16

Reason: Since, default capacity of StringBuffer is 16

OCJP4. Try at Home [Inside PSVM] - What is the default capacity of StringBuffer
StringBuffer sb=new StringBuffer();
System.out.println(sb.capacity());

Ans: Default capacity 16 for both StringBuffer and StringBuilder

OCJP5. How we can find out number of characters in a given String variable?
(a) length (b) size (c) length() (d) size()

Ans: (c) length() - Reason - It is a built-in function inside java.lang.String


class

OCJP6. How we can find out number of items in a given array variable?
(a) length (b) size (c) length() (d) size()

Ans (a) Since, length is a property (C++: Data Member) of array.

OCJP7. Google Search - In Java, array is internally assumed as?


Ans: Object.
This is the reason why we are writing array.length like c.length

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

Feature of SCP - String Constant Pool


=>Each String is created only once.
=>Multiple variables can share it.
=>Benefit: Saving of memory-space (as copies are not created).

When we use => A new of copy of String get created.

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");

OCJP3: [Inside PSVM]


StringBuffer sb=new StringBuffer();
sb.append("A Quick Brown Fox");
System.out.println("Length=" + sb.length());
System.out.println("Capacity=" + sb.capacity());

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 IV: Right Click=>Fix Imports

Step V: after public class, add following line


Timer t=null; //Initialize with null
Step VI: RC=>Fix Imports=>javax.swing.Timer

Step VII: Inside


public SplashFrame(){
initComponents(); //Pre-written code
//Add Lines here (*2)
}
(*2)=>
t=new Timer(1000,this); //1000 millisec=1sec, event handler
t.start(); //Start the timer to raise

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);
}

Step IX: RC=>Fix Imports

Step X: RC=>Run File


OR
LoginFrame=>Run File

You might also like