Download presentation
Presentation is loading. Please wait.
Published byMarlene Pierce Modified over 8 years ago
1
Topics for today: 1.Comments 2.Data types 3.Variable declaration
2
COMMENTS Easiest, so let’s start with them.
3
Comments Annotation (notes) about the code. Computer only executes code.
4
Comments Annotation (notes) about the code. Computer only executes code. Comments are for human readers of our code. How/what/why about the code that follows (so comments appear before the code that they describe).
5
Comments We need a way to tell the compiler that this is a comment and not code (so the compiler can ignore our comment). // This is a one line comment. /* This is a block comment. It may span multiple lines. */ /* Or it may be just a single line. */
6
DATATYPES
7
Data types In math we may say, “Let x be an integer.” Or, “Let a be a real number.” Note there is a difference between an integer and a real number.
8
Data types In math we may say, “Let x be an integer.” Or, “Let a be a real number.” How can we do this in our Java code?
9
Data types Predefined data types: intan integer doublea real number Stringa string of characters chara single character and many others
10
Data types Predefined data types: intan integer doublea real number Stringa string of characters chara single character many others Classes (like String, System, and Scanner) are data types as well!
11
VARIABLE DECLARATION
12
Variable declaration So how do we say, “Let x be an integer?” int x; This is a variable declaration. x is the name of the variable. We made up this name. int is a keyword or reserved word. It is part of the Java language. We can’t use int for anything else (like variable names). We are required to declare x. We may (and need) only declare x once.
13
Variable declaration “Let x and y be integers.” int x; int y; or int x, y;
14
Variable declaration “Let x be an integer, and height be a real number.” int x; double height; What value does x have?
15
Variable declaration “Let x be an integer, and height be a real number.” int x; double height; What value does x have? The value of x has not been specified yet. (We say that, “x has not been initialized.”) In Java, it is an error to use x before initialization.
16
Variable declaration: memory map int x; double height;
17
Variable declaration and assignment //declare vars int x; String name; //note uppercase 'S' //assign initial values name = "fred";
18
Variable declaration and assignment //declare vars int x; String name; char ch; //assign initial values ch = 'x';
19
Variable declaration and assignment //declare vars int x; //assign initial values x = 12;
20
Variable declaration and assignment in one int x = 12; //two steps in one
21
I/O //declare I/O var Scanner s; /* Initialize I/O var (more complicated than ints because these are objects) */ s = new Scanner( System.in );
22
Variable names Variable names must be one character from the first set, and 0 or more characters from the second set: [a..z,A..Z,_,$][a..z,A..Z,0..9,_,$]*
23
Variable names Variable names must be one character from the first set, and 0 or more characters from the second set: [a..z,A..Z,_,$][a..z,A..Z,0..9,_,$]* aXxfred0 Xx1_ybirds2 2birdsa$b75 a+ba&b2"ethel"
24
Variable names Variable names must be one character from the first set, and 0 or more characters from the second set: [a..z,A..Z,_,$][a..z,A..Z,0..9,_,$]* “The $ character should be used only in mechanically generated source code or, rarely, to access preexisting names on legacy systems.” - http://java.sun.com/docs/books/jls/third_edition/html /lexical.html#3.8
25
Keywords (reserved words) class import static int double if for and many others
26
Next time: Arithmetic expressions Data I/O
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.