0% found this document useful (0 votes)
12 views4 pages

Java String Manipulation Techniques

The document summarizes topics to be covered in a lecture on working with strings in Java. It includes: 1) Converting a string to a character array and counting characters, 2) Converting to a byte array and displaying ASCII codes, 3) Discussing sample exam questions on strings, 4) Continuing work on a splash screen GUI application in Swing that uses a progress bar powered by a timer. Doubt clarification will also be provided.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

Java String Manipulation Techniques

The document summarizes topics to be covered in a lecture on working with strings in Java. It includes: 1) Converting a string to a character array and counting characters, 2) Converting to a byte array and displaying ASCII codes, 3) Discussing sample exam questions on strings, 4) Continuing work on a splash screen GUI application in Swing that uses a progress bar powered by a timer. Doubt clarification will also be provided.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

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 [Link];
public class CountWordExample{
public static void main(String args[ ]){
Scanner s=new Scanner([Link]);
[Link]("Enter a String ");
String n=[Link](); //It takes String with Spaces
//Now, convert String n to character array
char c[ ]=[Link]();
//Perform a loop from index 0 to length-1 and count number of 'a'
int count=0;
for(int i=0; i<[Link]; 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
[Link]("Total Number of a in %s is %d" , n , count);
} //main
} //class

import [Link];
public class CountWordExample{
public static void main(String args[ ]){
Scanner s=new Scanner([Link]);
[Link]("Enter a String ");
String n=[Link](); //It takes String with Spaces
//Now, convert String n to character array
char c[ ]=[Link]();
//Perform a loop from index 0 to length-1 and count number of 'a'
int count=0;
for(int i=0; i<[Link]; 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
[Link]("Total Number of a in %s is %d" , n , count);
} //main
} //class

import [Link];
public class ByteArrayExample{
public static void main(String args[ ]){
Scanner s=new Scanner([Link]);
[Link]("Enter a String ");
String n=[Link](); //It takes String with Spaces
//Now, convert String n to byte array
byte c[ ]=[Link]();
//Perform a loop from index 0 to length-1 and count number of 'a'
int count=0;
for(int i=0; i<[Link]; i++){
[Link](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) => [Link]([Link](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();
[Link]("TecDev");
[Link]([Link]()); //Output: 6
[Link]([Link]()); //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();
[Link]([Link]());

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 [Link]


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 [Link] like [Link]

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([Link](b))
S.o.p("a eq b");

if([Link](c))
S.o.p("a eq c");

OCJP3: [Inside PSVM]


StringBuffer sb=new StringBuffer();
[Link]("A Quick Brown Fox");
[Link]("Length=" + [Link]());
[Link]("Capacity=" + [Link]());

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=>[Link]

Step VII: Inside


public SplashFrame(){
initComponents(); //Pre-written code
//Add Lines here (*2)
}
(*2)=>
t=new Timer(1000,this); //1000 millisec=1sec, event handler
[Link](); //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=[Link]();
//Increase value by 1
v++;
//Check if reaches to 101
if(v==101){
[Link](); //Stop the Timer
//Leave a Line for Future use
[Link](); //Close this Frame
return; //Terminate Fn
}
//Otherwise, update value of jProgressBar
[Link](v);
}

Step IX: RC=>Fix Imports

Step X: RC=>Run File


OR
LoginFrame=>Run File

Common questions

Powered by AI

In Java, the 'length' is a property used for arrays to indicate the number of elements, whereas 'length()' is a method for String objects indicating the number of characters in the string. This reflects their different nature, as arrays are objects with a fixed property, whereas Strings provide method-based access to their properties as objects .

In Java, a string object can be created without the 'new' keyword, which allocates memory from the String Constant Pool (SCP). This allows multiple variables to share the string, saving memory space as copies are not created. Conversely, using the 'new' keyword allocates memory from the heap, creating a new copy of the string. The advantage of SCP is efficient memory utilization .

Converting a String to a character array allows for precise control over each character, enabling specific text processing tasks such as counting character occurrences, modifying individual characters, or analyzing patterns within the string. It simplifies looping through the string for tasks like counting specific characters, e.g., counting the number of 'a' characters .

Arrays in Java are handled as objects, which implies that they are treated with object-like properties such as reference and memory management via heap allocation. This impacts their manipulation since operations like passing arrays to methods involve passing the reference rather than copying contents, facilitating efficient data manipulation .

Converting a String to a byte array in Java is significant for data transmission over a network because data needs to be converted into a binary format (0s and 1s) for compatibility with transmission protocols like TCP/OSI Model .

The String class in Java is given special treatment because it facilitates efficient memory usage and performance optimizations. Strings can be stored in the String Constant Pool (SCP), allowing for memory-saving by sharing common strings and reducing duplication. This makes it easier to manipulate and manage strings effectively in applications .

Using a Swing Timer to implement a progress bar in Java applications enables synchronous user feedback without halting the execution thread. As the timer triggers events at set intervals, the progress bar visually updates, enhancing user experience by indicating ongoing operations like file loading or complex computations .

In Java, StringBuffer objects have a length and a capacity. The length is the number of characters currently stored, while the capacity denotes the storage ability before resizing is required. For an empty StringBuffer, the default capacity is 16. As characters are added, the capacity can automatically increase if needed .

Understanding the String Constant Pool (SCP) is crucial for optimizing Java application memory because SCP allows for reusable storage of immutable string objects. By storing only one copy of distinct strings, it minimizes memory footprint and helps manage large-scale applications where memory efficiency is key. It also reduces garbage collection overhead by avoiding redundancy .

While the document doesn't discuss Scala, generally, Scala offers more concise syntax and functional programming constructs that simplify concurrent task management, such as using futures or Akka library for actor-based concurrency model. These can provide more straightforward and less error-prone methods compared to Java's traditional thread-based concurrency [general knowledge].

You might also like