Download presentation
Presentation is loading. Please wait.
Published bySheryl Hutchinson Modified over 6 years ago
1
Crash course in the Java Programming Language
2
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 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" a message containing "Hello" and the name of the greeted person or entity. public String sayHello() return "Hello, " + name + "!";
3
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); }
4
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
5
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
6
Primitive Data Types Integer data types: int byte short long
Floating-point types: double float Character type: char Logical Type: boolean
7
Primitive Types
8
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!
9
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.
10
Operators and Precedence
( ) / * % prefix postfix < > <= >= == != && || = += -= /= *=
11
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 )
12
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();
13
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(" "); const double PI = ; System.out.println(fiveDecimals.format(PI));
14
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.)
15
Lab Time: Files and Exceptions Exercise: Work along with me.
On your USB drive: Create a NetBeans Projects directory Continues in Next Presentation
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.