Below are the primitive variable types in Java. There are also wrapper classes (Chapter 6) that allow primitive variables be "wrapped" into a class.
byte (1 byte) -128 to 127
short (2 bytes) -32,768 to 32,767
int (4 bytes) -2.1 billion to 2.1 billion
long (8 bytes) -9 quintillion to 9 quintillion
float (4 bytes) range depends on number of places past decimal
double (8 bytes) range depends on number of places past decimal
boolean (1 byte) 1 or 0 (true or false)
char (2 bytes) any ASCII or Unicode character
Below are examples of declaring and using variables.
short Value1 = 5, Value2 = 7;
int Sum = Value1 + Value2;
char MyGrade = 'A';
float Money = 5.75;
double Pi = 3.1416;
boolean Married = false;
The program below uses some of the primitive variable types.
// This Java program demonstrates some variable types public class variables { public static void main (String[] args) { double Wage = 12.25, Hours = 5.5, Pay; char Initial = 'D'; Pay = Wage * Hours; System.out.println(Initial + " gets paid $" + Pay); } } |
Output D gets paid $67.375 |
The basic mathematical operations are addition (+), subtraction (-), multiplication (*), division (/), and modulus (%). The table below demonstrates these operations.
Code | Output | Comments |
System.out.print(5 + 2 * 3); | 11 | Java follows the order of operations. |
System.out.print(20 % 7); | 6 | Modulus gives you the remainder in a division. When you divide 20 by 7, then 6 is the remainder. |
System.out.print(3 / 2); System.out.print(3.0 / 2.0); |
1 1.5 |
Remember to use decimal points in division. |
int A = 5; A++; System.out.print(A); |
6 | ++ is a shortcut to increment a variable.
A++ is the same as A = A + 1 |
int B = 5; B--; System.out.print(B); |
4 | -- is a shortcut to decrement a variable. B-- is the same as B = B - 1 |
int C = 5; C+=5; System.out.print(C); |
10 | This is the same as C = C + 5 |
int C = 5; C*=5; System.out.print(C); |
25 | This is the same as C = C * 5 |
double A = 2.5; int B = (int)A; System.out.print(B); |
2 | When converting from one variable type to another, you may need to cast (int). |
In Java, strings are represented as an instance of a class. Below is an example of declaring and using a string.
String Name = "Joshua";
System.out.println("My name is " + Name);
There are many methods for the string class. Some of these are demonstrated in the program below.
// This Java program demonstrates some of the string methods public class stringdemo { public static void main(String[] args) { String S1 = "lemon", S2 = "lime", S3 = " CHIPS "; System.out.println("1. Length of S1 = " + S1.length()); System.out.println("2. Character at position 3 of S1 = " + S1.charAt(3)); System.out.println("3. " + S2.toUpperCase()); System.out.println("4. I like " + S3.trim().toLowerCase() + " and salsa"); System.out.println("5. Location of 'o' in S1 = " + S1.indexOf('o')); System.out.println("6. Substring Example: " + S3.substring(3,5)); System.out.println("7. Replace Example: " + S1.replace("emo","iste")); } } |
Output 1. Length of S1 = 5 2. Character at position 3 of S1 = o 3. LIME 4. I like chips and salsa 5. Location of 'o' in S1 = 3 6. Substring Example: HI 7. Replace Example: listen |
Below is a list of the string methods. Assume the following declaration:
String A = "apple", B = "orange", C = "pear,grape", Age="21";
Method | Result | Description |
Length | ||
A.length() | 5 | The length of A |
Comparison | ||
A.equals(B) | false | True if the two strings are equal |
A.equalsIgnoreCase(B) | false | Same as above but ignores the case |
A.compareTo(B) | -14 | If A < B, returns < 0; If A == B; returns 0; If A > B, returns > 0 |
A.compareToIgnoreCase(B) | -14 | Same as above, but ignores the case |
A.startsWith("ap") | true | True if A begins with "ap" |
A.endsWith("x") | false | True if A ends with "x" |
Searching | ||
A.contains("pp") | true | True if A contains "pp" |
A.indexOf("p") | 1 | Location of first occurrence of "p" |
A.indexOf("e",3) | 4 | Location of first occurrence of "e" at or after position 3 |
A.lastIndexOf("p") | 2 | Location of last occurrence of "p" |
A.lastIndexOf("p",1) | 1 | Location of last occurrence of "p" at or before position 1 |
Getting Parts | ||
B.charAt(3) | n | Character at position 3 |
B.substring(1) | range | Substring from position 1 to end |
B.substring(1,4) | ran | Substring from position 1 to before position 4 |
String[] Parts
= C.split(",") |
Parts[0]="pear" Parts[1]="grape" |
Splits a string using the specified delimiter (e.g. a comma). The parts are placed into an array. |
Changing | ||
B.toUpperCase() | ORANGE | Converts to all uppercase |
B.toLowerCase() | orange | Converts to all lowercase |
B.trim() | orange | Removes leading and trailing spaces |
B.replace("an","xx") | orxxge | Replace all "an" substrings with "xx" |
int Age = 21; String MyAge = String.valueOf(Age); |
21 | Convert an integer to a string |
Arrays are used to store multiple values in a single variable. To declare and initialize an array in one line, you can do the following:
double[] HourlyWage = {17.25, 20.75, 22.0, 18.55, 20.0};
To access individual elements in the array:
HourlyWage[0] += 1.0; // Makes first element
18.25
HourlyWage[1] = HourlyWage[4]; // Makes second element
20.0
To declare a 10 element array without initializing:
int[] SomeNumber = new int[10]; // Besides variables, it can be an array of objects
To declare a 10x10 multidimensional array:
int[][] World = new int[10][10]; // 100 total integer variables declared in 2-diminsional array
Below is a program that steps through each element of an array using a loop. Notice use of the array.length variable.
public class array { public static void main (String[] args) { double[] HourlyWage = {17.25, 20.75, 22.0, 18.5, 20.0}; for(int i=0; i<HourlyWage.length; i++) { HourlyWage[i]++; System.out.println(HourlyWage[i]); } } } |
Output 18.25 21.75 23.0 19.5 21.0 |