Crash course in the Java Programming Language
Hello World in Java import java.util.*; /** A class for producing simple greetings. */ public class Greeter { private String name; Constructs a Greeter object that can greet a person or entity. @param aName the name of the person or entity who should be addressed in the greetings. public Greeter(String aName) name = aName; } Greet with a "Hello" message. @return a message containing "Hello" and the name of the greeted person or entity. public String sayHello() return "Hello, " + name + "!";
Hello World Tester import java.io.*; import java.util.*; public class GreeterTester { public static void main(String[] args) Greeter worldGreeter = new Greeter("World"); String greeting = worldGreeter.sayHello(); System.out.println(greeting); }
Identifiers (names) Java is case sensitive Identifiers can begin with an underscore or letter and contain only letters, underscores, or numbers We choose identifiers for variables, classes, methods, and packages
Identifiers variables must start with a letter or underscore consists of letters, digits & underscores cannot be a reserved word cannot be true, false, or null firstScore score1 Score 3scores score-1 myScore Capitalize “middle” words Usually start variables with lowercase, classes with uppercase – class name and file name must be identical. Java is case sensitive!!!! Score score
Primitive Data Types Integer data types: int byte short long Floating-point types: double float Character type: char Logical Type: boolean
Primitive Types
Declaring Variables Syntax: Examples: int num1; double num2, num3; type identifier1, identifier2, … ; Examples: int num1; double num2, num3; double num4; We must declare a variable before it can be used!
Assignment Statement Syntax: variable = expression; expression is evaluated and then the value of expression is stored in variable. We should initialize our variables. int num1 = 0; double num2 = 0.0, num3 = 0.0; Incorrect: int 0 = num1; In Java, 0 is treated as an integer while 0.0 is treated as a floating-point number. We may assign a value to a variable more than once. However, a variable can only retain its most recently assigned value.
Operators and Precedence ( ) / * % ++ -- prefix ++ -- postfix + - < > <= >= == != && || = += -= /= *=
Literals is a constant value that appears directly in a program. Character Data Type: represent a single character //initialize a character data type named “firstLetter” with value ‘A’ char firstLetter = 'A'; A character literal is enclosed in single quotation marks. Which of the following is a character literal? ‘a’ ‘A’ ‘ ‘ ‘$’ ‘7’ ‘ab’ ‘ a ‘ “a” A string literal must be enclosed in double quotation marks. The String type String a = “Chapter”; String b = “Chapter” + 2; (note: please ignore the smart quotes )
Standard I/O import java.util.*; ... System.out.print( ); System.out.println() // must declare a scanner object for input Scanner keyboard=new (System.in); someInt = keyboard.nextInt(); somestr = keyboard.next(); someReal= keyboard.nextDouble();
Formatting Output import java.text.*; DecimalFormat moneyStyle = new DecimalFormat("$0.00"); double amount = 4.56; System.out.println(moneyStyle.format(amount)); ====================================================== DecimalFormat fiveDecimals = new DecimalFormat("0.00000"); const double PI = 3.14159; System.out.println(fiveDecimals.format(PI));
If Statement Syntax: if (BooleanExpression) Single Java Statement; where Statement is any Java statement including a block {}. Semantics: If BooleanExpression is true, then execute the Single Java Statement. (Otherwise, skip the Statement.)
Lab Time: Files and Exceptions Exercise: Work along with me. On your USB drive: Create a NetBeans Projects directory Continues in Next Presentation