0% found this document useful (0 votes)
79 views12 pages

Programming 1A: (PROG5121)

This document discusses Java programming concepts related to characters, strings, and number formatting. It covers the char data type, character class methods like toLowerCase() and isLetterOrDigit(), string immutability and how to modify strings using StringBuilder/StringBuffer, string capacity allocation, converting between strings and numeric types, and using DecimalFormat for number formatting. Examples are provided to demonstrate these programming concepts.

Uploaded by

Dzudzi Manyuha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views12 pages

Programming 1A: (PROG5121)

This document discusses Java programming concepts related to characters, strings, and number formatting. It covers the char data type, character class methods like toLowerCase() and isLetterOrDigit(), string immutability and how to modify strings using StringBuilder/StringBuffer, string capacity allocation, converting between strings and numeric types, and using DecimalFormat for number formatting. Examples are provided to demonstrate these programming concepts.

Uploaded by

Dzudzi Manyuha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

PROGRAMMING 1A

(PROG5121)
CHARACTERS AND STRINGS

BASED ON CHAPTER 7, FARRELL, J. 2019. JAVA PROGRAMMING.


9TH ED. 2019. COURSE TECHNOLOGY, CENGAGE LEARNING.
THE CHARACTER DATA TYPE REVISITED
class myClass {
The char data type holds a character.
public static void main(String[] args) {

char myCharacter = ‘A’;

System.out.println(myCharacter);

(01000001)2 = 1x26 + 1x20 = 64 + 1 = 6510


(41)16 = 4x161 + 1x160 = 6510
THE CHARACTER CLASS METHODS
USING THE CHARACTER CLASS
public class CharacterInfo { aChar = Character.toLowerCase(aChar);

public static void main(String[] args) { System.out.println("After toLowerCase(), aChar is " + aChar);

char aChar = 'C’; aChar = Character.toUpperCase(aChar);

System.out.println("The character is " + aChar); System.out.println("After toUpperCase(), aChar is " + aChar);

if (Character.isUpperCase(aChar)) if (Character.isLetterOrDigit(aChar))

System.out.println(aChar + " is uppercase"); System.out.println(aChar + " is a letter or digit");

else else

System.out.println(aChar + " is not uppercase"); System.out.println(aChar + " is neither a letter nor a digit");

if (Character.isLowerCase(aChar)) if (Character.isWhitespace(aChar))

System.out.println(aChar + " is lowercase"); System.out.println(aChar + " is whitespace");

else else

System.out.println(aChar + " is not lowercase"); System.out.println(aChar + " is not whitespace");

}
Continued on the right …
}
STRING DATA PITFALLS
import java.util.Scanner;

public class TryToCompareStrings { • in Java, String is a class, and each created


String is an object.
public static void main(String[] args) {
• As an object, a String variable name is not a
String aName = "Carmen"; // String s=new String(“Carmen”); simple data type - it is a reference; that is, a
variable that holds a memory address.
String anotherName; • Therefore, when you compare two String
objects using the == operator, you are not
Scanner input = new Scanner(System.in); comparing their values, but their computer
memory locations.
System.out.print("Enter your name > ");

anotherName = input.nextLine();

if (aName == anotherName)

System.out.println(aName + " equals " + anotherName);

else

System.out.println(aName + " does not equal " +


anotherName);

}
STRING OBJECTS ARE IMMUTABLE

• String objects are never


actually changed.
• What happens instead is:
 A new String object is
created in memory.
 Its address is now
stored in the String
variable.
• Strings and other objects
that can’t be changed are
immutable.
JAVA SOLUTION TO IMMUTABLE STRINGS
• StringBuilder and StringBuffer are both mutable classes that can be used to do operations on string
objects; e.g., to reverse a string, concatenate a string, etc.

• With these classes we can modify a string without creating a new object of the string.

• StringBuffer is thread-safe whereas StringBuilder is not thread-safe.  Therefore, it is faster than a string
buffer. Also, the string concatenation operator internally uses StringBuffer or StringBuilder. 

public class StringBuilderExample { public class StringBufferExample {

public static void main(String[] args) { public static void main(String[] args) {

StringBuilder builder = new StringBuilder("Hi"); StringBuffer buffer = new StringBuffer("Hi");

builder.append("Java 13"); buffer.append("Java 13");

System.out.println("StringBuilderExample" + builder); System.out.println("StringBufferExample" +


buffer);
}
}
}
}
STRING CAPACITY ALLOCATION
import javax.swing.JOptionPane; int addStringCapacity = addressString.capacity();

public class StringBuilderDemo { System.out.println("Capacity of addressString is " +

public static void main(String[] args) { addStringCapacity);

StringBuilder nameString = new StringBuilder("Barbara"); nameString.setLength(20);

int nameStringCapacity = nameString.capacity(); System.out.println("The name is " + nameString + "end");

System.out.println("Capacity of nameString is " + addressString.setLength(20);

nameStringCapacity); System.out.println("The address is " + addressString);

StringBuilder addressString = null; }

addressString = new }

StringBuilder("6311 Hickory Nut Grove Road");

Continued on the right …

Whenever you create a StringBuilder object using a String


as an argument to the constructor, the StringBuilder’s
capacity is the length of the String contained as the
argument to the StringBuilder, plus 16.
STRING TO INTEGER CONVERSION

String myString = "25";


Integer.parseInt() returns the int to String conversion:
try { string as a primitive type int. int i = 10;
String s = String.valueOf(i);
int number = Integer.parseInt(myString);
Integer to String conversion:
System.out.println(number); // Output = 25 int i = 10;
String myString = "25"; String s = Integer.toString(i);
}
try {
catch (NumberFormatException nfe) {
Integer number = Integer.valueOf(myString);
nfe.printStackTrace();
System.out.println(number); // Output = 25
}
}

catch (NumberFormatException nfe) {


Integer.valueOf() returns an Integer nfe.printStackTrace();
object which is equivalent to a new
Integer(Integer.parseInt(s)). }
OTHER STRING/NUMBER CONVERSIONS
long l = Long.parseLong("-11111"); // String to long.

Long num = new Long("10000"); // String to Long.

float f = Float.parseFloat("23.6"); // String to float.

Float d = new Float("2033.12244"); // String to Float.

double d = Double.parseDouble("23.6"); // String to double.

Double d = new Double("2033.12244"); // String to Double.

String s = String.valueOf(567L); // Long to String.

String s = Long.toString(897L); // Long to String.

String s = String.valueOf(12.3F); // Float to String.

String s = Float.toString(89.7F); // Float to String.

String s = String.valueOf(123.456D); // Double to String.

String s = Double.toString(123.45D); // Double to String.


CLASS ACTIVITY 7
Write a Java class called DecimalFormatExample with a main method that creates a DecimalFormat object,
myFormatter, by passing a pattern string to the DecimalFormat constructor.

The format() method, which DecimalFormat inherits from NumberFormat, must be invoked by myFormatter - it must
accept a double value as argument and return the formatted number in a string.

The output of your program must looks as follows:

Number: 123456.789 Pattern: ###,###.### Formatted Number: 123,456.789

Number: 123456.789 Pattern: ###.## Formatted Number: 123456.79

Number: 123.78 Pattern: 000000.000 Formatted Number: 000123.780

Number: 12345.67 Pattern: R###,###.### Formatted Number: R12,345.67

You might also like