Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
152 views

JAVA Arrays and Strings

This document discusses arrays, strings, and string operations in Java. It covers: - How to define and iterate over arrays using a for-each loop - That strings are immutable and should be initialized with "=" not "new" - Demonstrates representing single characters with char and multiple with String - Introduces StringBuilder for mutable string operations like append and insert

Uploaded by

Kris kris
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
152 views

JAVA Arrays and Strings

This document discusses arrays, strings, and string operations in Java. It covers: - How to define and iterate over arrays using a for-each loop - That strings are immutable and should be initialized with "=" not "new" - Demonstrates representing single characters with char and multiple with String - Introduces StringBuilder for mutable string operations like append and insert

Uploaded by

Kris kris
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

JAVA Arrays and Strings

Arrays and for-each Loop


Arrays and for-each Loop

 This class demonstrates how to define arrays in Java and iterate over arrays by using
for- each loop.
 The first array, names, creates a String array and initializes each element separately. The
second array, numbers is an array of integers.
 Each array is iterated through using the Java for-each construct
 The enhanced for loop is used to make the for loop more compact and easy to read
 It is used to access each successive value in a collection of values. It is commonly used
to iterate over an array or a Collections class (for example, an Array or an ArrayList)
Strings
 The String class is immutable
 After being created, the contents of an object of the class String can never be modified
 The immutability of String objects helps the JVM to reuse String objects, reducing memory
overhead and improving performance.
 Strings should always be initialized by using the assignment operator "=" and text in
quotation marks, as shown in the examples.
 The use of new to initialize a String is strongly discouraged.
 The reason is that "Bad Practice" in line 10 is a String literal of type String.
 Using the new keyword simply creates another instance functionally identical to the literal.
 If this statement appeared inside of a loop that was frequently invoked, a lot of needless
String instances could be created.
Strings
Strings

The code in the slide demonstrates how text characters are


represented in Java. Single characters can be represented
with the char type. However, Java also includes a String type
for representing multiple characters. Strings can be defined
as shown in the slide and combined by using the "+" sign as
a concatenation operator.
String Operations: StringBuilder
String Operations: StringBuilder

 The slide demonstrates usage of the StringBufilder class and commonly


used methods.
 The StringBuilder objects are like String objects except that they may be
modified (mutable).
 The StringBuilder class is used when dealing with larger strings or
modifying the contents of a string often.
 The example in this slide demonstrates modifying the String by using the
append and insert methods and chaining by using the append method.

You might also like