Java Basics
Last modified Wednesday, 11-Sep-2024 17:59:44 UTC.
Object Oriented Programming Notions
Programs, classes, and objects (
Slides
)
The Big Picture
A program consists of classes
Classes might be packaged
Classes have static parts and non-static parts
Static parts always exist
Non-static parts define what objects look like
Objects are created with the
new
operator
Classes have data and methods
Program components
Files
Comments
import
statments
Class declarations
Method declarations
Statements
Simple Java programs
One file
One public class (same name as file)
One
main
method
Class methods and final class data as required
Using existing classes
To become a good object-oriented programmer, one must first learn how to use predefined classes.
The
import
statement has the syntax
import <package hierarchy>.<class name>
The
<class name>
can be
*
to mean "all classes"
Classes from the
Java class library
.
General purpose classes in
java.lang
All classes in
java.lang
are automatically imported by
javac
.
The
Object
class contains very general purpose things
The
System
class contains
out
with methods for printing on the screen.
System.out.print
prints and leaves the cursor at the end of the output.
System.out.println
prints and moves the cursor to the start of the next line.
PrintScanPrint.java
The
String
class contains string manipulation.
The
Scanner
class in
java.util
contains methods for reading data from the keyboard
String next()
- Reads the next word.
String nextLine()
- Reads until the end of the line.
byte nextByte()
,
short nextShort()
,
int nextInt()
,
long nextLong()
- Reads the next integer as a
byte
,
short
,
int
, or
long
.
float nextFloat()
,
double nextDouble()
- Reads the next real number as a
float
or
double
.
boolean nextBoolean()
- Reads the next
boolean
.
Exercises
What Java operator is used to create objects?
What are the two different types of data in Java?
What is the main difference between a static and non-static data value.
What are local variables? Where can they be used?
Describe the three forms of comments in Java.
How many public classes should there be in a file containing the
main
method?
Exam Style Questions
Explain the difference between the
static
and
non-static
parts of a class.
What two types of things are classes and objects composed of?
What are
import
statements used for?