Ch02-Java Fundamentals
Ch02-Java Fundamentals
Through Objects
Eighth Edition
Chapter 2
Java Fundamentals
• This line uses the System class from the standard Java
library.
• The System class contains methods and objects that
perform system level tasks.
• The out object, a member of the System class, contains
the methods print and println.
Will output:
These lines will beprinted onthe same line.
Notice the odd spacing? Why are some words run together?
+9,223,372,036,854,775,807
float 4 bytes Floating-point numbers in the range of
± 3.410
+ or 3838toto +±or 3.41038,
−minus
minus 3.410 minus 3.41038, with 7 digits of accuracy
double 8 bytes Floating-point numbers in the range of
± 1.710
+ or − 308
minus 1.710 minus 308 minus 1.710308, with 15 digits of accuracy
toto±+ or1.710308,
Copyright © 2022 Pearson Education, Inc. All Rights Reserved
Variable Declarations
• Variable Declarations take the following form:
– DataType VariableName;
▪ byte inches;
▪ short month;
▪ int speed;
▪ long timeStamp;
▪ float salesCommission;
▪ double distance;
2,900,000 2.9E6
2.9 times 10 to the sixth power
2.9 × 106
-
Negativ e sym bol.
Priority
Lower
Priority x = 6 + 3 − 4
Left to right 23
+- + 6 * 3;
+ minus symbols
• This is the method that all other objects must use when
they are created.
See example: StringDemo.java
Copyright © 2022 Pearson Education, Inc. All Rights Reserved
The String Methods
• Since String is a class, objects that are instances of it
have methods.
• One of those methods is the length method.
stringSize = value.length();
• Other examples:
var interestRate = 12.0;
var stockCode = "D465U";
var limit = 1000L;
• After this code executes, interestRate will be a
double, stockCode will be a String, and limit
will be a long
• Limitations:
– var can be used only to declare local variables
(variables that are declared inside a method)
– You cannot use var to declare multiple variables in
the same statement
– You must provide an initialization value when you
declare a variable with var
import java.util.Scanner;