Java Lesson 2 - Variable Types
Videos |
|
Text |
|
Three variable types:
(A good way to learn the following points is to modify the code of the “Hello World” program according to the suggestions below.)
1. String….used to store things in quotes….like “Hello world”
(A good way to learn the following points is to modify the code of the “Hello World” program according to the suggestions below.)
1. String….used to store things in quotes….like “Hello world”
public static void main(String args[])
{
String s = "Hello cruel world";
System.out.println(s);
}
2. int ….used to store integers (positive or negative)
public static void main(String args[])
{
int age = 59;
System.out.println(age);
}
3. double ….used to store “floating point” numbers (decimal fractions). double means “double precision”.
public static void main(String args[])
{
double d = -137.8036;
System.out.println(d);
d = 1.45667E23; //Scientific notation…means 1.45667 X 1023
}
Declaring and Initializing:
When we say something like
When we say something like
double x = 1.6;
we are really doing two things at once. We are declaring x to be of type double and we are initializing x to the value of 1.6. All this can also be done in two lines of code (as shown below) instead of one if desired:
double x; //this declares x to be of type double
x = 1.6; //this initializes x to a value of 1.6
What’s legal and what’s not:
int arws = 47.4;
//illegal, won’t compile since a decimal number cannot “fit” into //an integer variable.
double d = 103;
//legal…same as saying the decimal number 103.0
Rules for variable names:
Variable names must begin with a letter (or an underscore character) and cannot contain spaces. The only “punctuation” character permissible inside the name is the underscore (“_”). Variable names cannot be one of the reserved words (key words…see Appendix A) that are part of the Java language.
Variable names must begin with a letter (or an underscore character) and cannot contain spaces. The only “punctuation” character permissible inside the name is the underscore (“_”). Variable names cannot be one of the reserved words (key words…see Appendix A) that are part of the Java language.
Legal Names
|
Illegal names
|
Variable naming conventions:
It is traditional (although not a hard and fast rule) for variable names to start with a lower case letter. If a variable name consists of multiple words, combine them in one of two ways:
bigValue… jam everything together. First word begins with a small letter and subsequent words begin with a capital.
big_value… separate words with an underscore.
It is traditional (although not a hard and fast rule) for variable names to start with a lower case letter. If a variable name consists of multiple words, combine them in one of two ways:
bigValue… jam everything together. First word begins with a small letter and subsequent words begin with a capital.
big_value… separate words with an underscore.
Here are the rules concerning println and print:
- System.out.println( ) completes printing on the current line and pulls the print position down to the next line where any subsequent printing continues.
- System.out.print( ) prints on the current line. Any subsequent printing continues on the current line.
Assignments |
|