Data Type Default Value (For Fields)
Data Type Default Value (For Fields)
html
RANGE -128 to 128 -32,768 to 32,768 -2,147,483,648 to 2,147,483,648 -9,223,372,036,854,775,808 to 9,223,372,036,854,775,808 Number with a decimal BIG NUMBERS! (decimals) TRUE or FALSE \u0000 (or 0) to \uffff (or 65,535 Used for characters like a or z
char letter = c
It is not necessary to declare a value when declaring a type. The compiler will declare a default value(but this is considered bad programming style and can lead to complications if not kept in check: Data Type byte short int long float double char Default Value (for fields) 0 0 0 0L 0.0f 0.0d '\u0000'
null false
Green = the actual code you use Blue = example used to clarify the code Red = template for the code TO DO: Print to the console (The bottom of the screen, not to the windows GUI (Graphical User Interface) USE THIS CODE System.out.println("Hello World"); ~ println adds a newline character after the printout System.out.print("Hello World")); ~ print prints on the same line wherever the output left off. System.out.println("Hello " + name + "/n" ); NOTES System.outprintln is a basic code you will use for the entire semester. You can put a string literal (something in parenthesis) to print that exact thing to the console, and you can concatenate literals and variables and escape codes (such as the newline command) in the parenthesis to get a more advanced output. See the above table for a list of the
Declare a variable
int daysWorked = 7; double payrate = 8.25; String name = "Matthew"; char choice = 'a'; int day, week, month = 12;
Display a message in a dialog box through the windows GUI Prompt the user for input through the windows GUI
types. Make sure you use the smallest type to haold your variable. You dont want to use a double to hold a number like 100. That will waste memory. You can also declare more than one variable of the same type in the same line. remember to include the Swing library with an include statement
import javax.swing.*;
String str = JOptionPane.showInputDialog(null, "Enter some text : ", "Swing Input", 1);
EXAMPLE: int first, second; first = Integer.parseInt(JOptionPane.showInputDialog("Enter First Integer: ")); second = Integer.parseInt(JOptionPane.showInputDialog("Enter Second Integer: ")); (change)variablename
Typecast (primitive types) Parse a String to convert input from a user into another type so you can do operations on it
(int)hoursWorked
Integer.parseInt(stringName) Double.parseDouble(stringName)
Iteration for
The for loop uses an increment or decrement to keep going. * The for loop will always go at least once.
while
do while
The while loop will only execute whenever the condition is TRUE. It also used an increment to repeat. It may or may not execute, depending on whether the condition is true The do while loop tests the condition at the end of the loop, so it will always go at least once.
if-thenelse
if (testscore >= 90) { grade = 'A'; } else if (testscore >= 80) { grade = 'B'; } else if (testscore >= 70) { grade = 'C'; } else if (testscore >= 60) { grade = 'D'; } else { grade = 'F'; }
Switch Statement
switch (month) { case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10: case 11: case 12: default:
monthString break; monthString break; monthString break; monthString break; monthString break; monthString break; monthString break; monthString break; monthString break; monthString break; monthString break; monthString break; monthString break;
= "January"; = "February"; = "March"; = "April"; = "May"; = "June"; = "July"; = "August"; = "September"; = "October"; = "November"; = "December"; = "Invalid month";
The switch statement is used for multiple options, like a menu. You can create the same result of a switch statement with an if-then-else statement, but a switch statement is cleaner.
} System.out.println(monthString); }
Anatomy of a basic Program //Basic Program //Matthew Gazdich // public class HelloWorld{ public static void main(String arg[]){ String name = "Matthew"; System.out.println("Hello " + name + "\n"); } }