D.3.4 Describe the uses of the primitive data types and the reference class string.
Java programming language is statically-typed, which means that all variables must first be declared before they can be used. This involves stating the variable's type and name.
A primitive type is predefined by the language and is named by a reserved keyword.
Primitive values do not share state with other primitive values.
The eight primitive data types supported by the Java programming language are:
byte (8 bits) - Numerical (Integer values)
int (32 bits) - Numerical (Integer values)
long (64 bits) int (32 bits) - Numerical (Integer values)
double (64 bits)
float (32 bits)
char (16 bits)
boolean (1 bit)
int length = 10;
tells your program that a field named "length" exists,
holds numerical data, and has an initial value of "10".
A variable's data type determines the values it may contain, plus the operations that may be performed on it.
Integer underflow error
Occurs when an integer value becomes less than the minimum value that can be represented in its type. This often causes a negative value to become positive, causing the source code to not execute as expected.
Integer overflow error
Occurs when an integer value becomes greater than the maximum value that can be represented in its type. This often causes a positive value to become negative, causing the source code to not execute as expected.
Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects.
The most direct way to create a string is to write:
String greeting = "Hello world!";
"Hello world!" is a string literal—a series of characters in your code that is enclosed in double quotes.
Whenever it encounters a string literal in your code, the compiler creates a String object with its value—in this case, Hello world!.
Enclosing your character string within double quotes will automatically create a new String object;
String s = "this is a string";
String name = "John Doe";
String objects are immutable, which means that once created, their values cannot be changed.
The String class is not technically a primitive data type, but considering the special support given to it by the language, you'll probably tend to think of it as such. You'll learn more about the String class in Simple Data Objects
length() - returns an integer corresponding to the number of characters in the string.
substring(int start, int end) - returns a substring of the string, which starts at start position and ends one character before the end position.
substring(int start)- returns a substring of the string, which starts at start position and extends to the end of the string.
toLowerCase()- returns a copy of the string with all lowercase letters.
toUpperCase()- returns a copy of the string with all uppercase letters.
indexOf(String str)- returns the integer corresponding to the location of the first occurrence of str in the string. Otherwise –1 is returned.
lastIndexOf(String str)- returns the integer corresponding to the location of the last occurrence of str in the string. Otherwise –1 is returned.
charAt(int i) - returns the character at index i
toCharArray(String s) - Converts this string to a new character array. Returns a newly allocated character array whose length is the length of this string and whose contents are initialized to contain the character sequence represented by this string.
trim()- returns a copy of the string with all leading and trailing spaces removed.
replaceFirst(String str1, String str2) - returns a string with the first occurrence of str1 replaced by str2.
replaceAll(String str1, String str2) - returns a string with all occurrences of str1 replaced by str2.
//declaring and assigning value to phrase variable
String phrase = "Java Programming";
//printing value of phrase variable
System.out.println("Phrase: " + phrase);
//printing all lowercase version of phrase
System.out.println("Phrase in lowercase: " + phrase.toLowerCase());
//printing all uppercase version of phrase
System.out.println("Phrase in uppercase: " + phrase.toUpperCase());
//print the number of characters (length) of phrase
System.out.println("Length of phrase: " + phrase.length());
//printing the first character of phrase
System.out.println("First character of phrase: " + phrase.charAt(0));
//printing the last character of phrase
System.out.println("Last character of phrase: " + phrase.charAt(phrase.length() - 1));
//printing the first word of the phrase
System.out.println("First word of the phrase: " + phrase.substring(0, phrase.indexOf(" ")));
//printing the last word of the phrase
System.out.println("Second word of the phrase: " + phrase.substring(phrase.indexOf(" ") + 1));
equals(String str) - returns true when the string is the same as str. Returns false otherwise.
equalsIgnoreCase(String str) - same as equals() except that uppercase and lowercase differences between the strings are ignored.
startsWith(String str) - returns true when the string begins with str.Returns false otherwise.
endsWith(String str) - returns true when the string ends with str. Returns false otherwise.
compareTo(String str) - returns 0 when str is the same as the string, a negative integer is returned when str comes alphabetically after the string, and a positive integer is returned when str comes alphabetically before the string. Note that uppercase and lowercase letters are considered different.
compareToIgnoreCase(String str) - same as compareTo() except that uppercase and lowercase differences between the strings are ignored.