Skip to content

Commit 03add68

Browse files
committed
String Intro and Update
1 parent 4a676d4 commit 03add68

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

README.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,8 @@ Decision Making in programming is similar to decision-making in real life. In pr
431431

432432
- A nested if is an if statement that is the target of another if or else. Nested if statements mean an if statement inside an if statement. Yes, java allows us to nest if statements within if statements. i.e, we can place an if statement inside another if statement.
433433

434+
Syntax Code :
435+
434436
```
435437
class Class_name{
436438
public static void main(String[] args){
@@ -467,6 +469,8 @@ class Class_name{
467469

468470
- The switch statement is a multiway branch statement. It provides an easy way to dispatch execution to different parts of code based on the value of the expression.
469471

472+
Syntax Code :
473+
470474
```
471475
class Class_name{
472476
public static void main(String[] arg){
@@ -502,6 +506,8 @@ Looping in programming languages is a feature which facilitates the execution of
502506

503507
- while Loop : A while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The while loop can be thought of as a repeating if statement.
504508

509+
Syntax Code :
510+
505511
```
506512
while(condition){
507513
// code block
@@ -519,6 +525,8 @@ for(initiator;condition;increment){
519525

520526
- do-while loop : do while loop is similar to while loop with only difference that it checks for condition after executing the statements, and therefore is an example of Exit Control Loop.
521527

528+
Syntax Code :
529+
522530
```
523531
do{
524532
// code here
@@ -537,6 +545,84 @@ Jump Statements are those statements which are used to come out of a particular
537545

538546
Note : Proper code is beeng given in [loops.java](https://github.com/ackwolver335/Java-Coder/blob/main/loops.java) file above !
539547

548+
## Strings in JAVA
549+
550+
Strings are the type of objects that can store the character of values and in Java, every character is stored in 16 bits i,e using UTF 16-bit encoding. A string acts the same as an array of characters in Java.
551+
552+
### Ways to create a String :
553+
554+
- String Literals : This methods of creating string is used in a situation in order to make java more memory efficient, cause in this method objects of the strings are not created.
555+
556+
Syntax Code :
557+
558+
```
559+
class Sample{
560+
public static void main(String[] a){
561+
String str1 = "Constant value String";
562+
}
563+
}
564+
```
565+
566+
- Using new keyword : In these type of cases the JVM used to create seperate objects in normal heap memo while the literals are placed in the string constant pool. And the variable created together with the object in the heap (Non-pool).
567+
568+
Syntax Code :
569+
570+
```
571+
class Sample{
572+
public static void main(String[] a){
573+
String s1 = new String("String inside Heap Non-Pool");
574+
}
575+
}
576+
```
577+
578+
### Categories of String
579+
580+
- **String** : String is an immutable class which means a constant and cannot be changed once created and if wish to change , we need to create an new object and even the functionality it provides like toupper, tolower, etc all these return a new object , its not modify the original object. It is automatically thread safe.
581+
582+
Syntax Code :
583+
584+
```
585+
String s1 = "String without Object"; // Method 1
586+
String s2 = new String("String with Object !"); // Method 2
587+
```
588+
589+
- **StringBuffer** : StringBuffer is a peer class of String, it is mutable in nature and it is thread safe class , we can use it when we have multi threaded environment and shared object of string buffer i.e, used by mutiple thread.
590+
591+
Syntax Code :
592+
593+
```
594+
StringBuffer str_bfr = new StringBuffer("Buffered Mutable String");
595+
```
596+
597+
- **StringBuilder** : StringBuilder in Java represents an alternative to String and StringBuffer Class, as it creates a mutable sequence of characters and it is not thread safe. It is used only within the thread , so there is no extra overhead , so it is mainly used for single threaded program.
598+
599+
Syntax Code :
600+
601+
```
602+
StringBuilder str_build = new StringBuilder();
603+
str_build.append("Ack Wolver"); // Adding some extras to the string
604+
```
605+
606+
- **StringTokenizer** : StringTokenizer class in Java is used to break a string into tokens. A StringTokenizer object internally maintains a current position within the string to be tokenized. Some operations advance this current position past the characters processed.
607+
608+
Syntax Code :
609+
610+
`
611+
StringTokenizer str1 = new StringTokenizer("Hello World! Having Space !"," ");
612+
while(str1.hasMoreTokens())
613+
System.out.println(str1.nextToken());
614+
`
615+
616+
- **StringJoiner** : StringJoiner is a class in java.util package is used to construct a sequence of characters(strings) separated by a delimiter and optionally starting with a supplied prefix and ending with a given suffix.
617+
618+
Syntax Code :
619+
620+
```
621+
StringJoiner str1 = new StringJoiner(",");
622+
sj1.setEmptyValue("It is empty");
623+
System.out.println(sj1);
624+
```
625+
540626
### Social Media Links :
541627

542628
- [Instagram Page](https://www.instagram.com/coding.needs/)

0 commit comments

Comments
 (0)