| title |
|---|
Java Fundamentals |
- A Java source code file:
- Contains one or more Java classes
- In this class we'll generally have one class per file
- Can only have 1 public class.
- The public class and the filename of the source code file must match
- i.e., A class named
Simplemust be inSimple.java
- i.e., A class named
- The public class and the filename of the source code file must match
- Comments: Are ignored by the compiler
//and/* */
- Class Header: Tells the compiler things about the class such as what other classes can use it (
private/public), that it is a Java class (class), and its name (simple).- All methods and data for a class go in-between its respective curly braces.
- e.g.,
public class Simple {}Java statements, curly braces, comments, and Java framework code don't need semicolons.
The console window displays only text.
- aka: The standard output device.
System.out.println("Programming is fun!");- This line uses the
Systemclass's$\to$ outobject's$\to$ printlnmethod to output text to standard output.- Note:
printlnAppends a newline (\n) whileprintdoes not
- Note:
The standard input device is typically the keyboard
- Java's standard Java library implements standard input.
| Char | Name | Description |
|---|---|---|
\n |
newline | Advances cursor to next line |
\t |
tab | Causes cursor to skip to next tab stop |
\b |
backspace | Causes cursor to back up, or move left, one position |
\r |
carriage return | Causes cursor to go to beginning of current line |
\\ |
backslash | Causes a backslash to be printed |
\' |
single quote | Causes a single quotation mark to be printed |
\" |
double quote | Causes a double quotation mark to be printed |
With these escape sequences, complex text output can be achieved.
- Variable: Named storage location in the computer's memory
- Literal: Value that is written into the code of a program
public class Variable
{
public static void main(String[] args)
{
int value; // Variable declaration
value = 5; // Assignment statement (stores '5' in memory)
System.out.print("The value is "); // "The value is " is a string literal
System.out.println(value); // Prints the integer '5'
}
}+ can be used two ways:
- As an addition operator
- e.g.,
5 + 5, yields10
- e.g.,
- As a concatenation operator
- If either side of the
+operator is aString, the result will be aString. - e.g.,
"5" + 5, yields"55"
- If either side of the
Warnings on String Literals:
- A string literal value cannot span lines in a Java source code file.
- Be careful with quotes, you may assign the wrong data type or mess up the syntax.
Identifiers: Programmer-defined names that represent classes, variables, or methods
- Cannot be any Java reserved keywords
- Should be self-documenting (e.g.,
int numberStudentsrather thanint nS) - May only contain
a-z,A-Z,0-9,_, or$- First char may not be a digit
- May not contain a space (
)
Naming Conventions:
- Variables and Methods: lowerCamelCase
- Class Names: UpperCamelCase
- General rule of thumb for naming variables and classes is to stick to nouns or noun phrases.
Primitive Data Type: The type of data the variable can hold
- Each variable has a data type
- Determines the amount of memory the variable uses
- You cannot use them to create objects (can only hold a single value, no attributes or methods)
Numeric Data Types:
| Name | Size | Range |
|---|---|---|
| byte | 1 byte | Integers in range -128 to 127 |
| short | 2 bytes | Integers in the range -32768 to 32767 |
| int | 4 bytes | Integers in the range -2147483648 to 2147483647 |
| long | 8 bytes | Integers in the range -9223372036854775808 to 9223372036854775807 |
| float | 4 bytes | |
| double | 8 bytes |
Tip: You can declare several variables of the same type like so:
int length, width, areaData types that allow fractional values.
Java Floating-Point Data Types:
float: Single-precision, 7 decimal points- Can be represented in scientific notation.
double: Double precision, 15 decimal points
Note: Remember to append
Fto a float.
Can have two possible values: true or false.
- Takes 1 bit of RAM
boolean bool;
bool = true;Provides access to single characters
charliterals are enclosed in single quotation marks
char letter;
letter = 'A';- Characters are stored as Unicode numbers
- Each character requires 2 bytes of RAM
char letter = 65;
System.out.println(letter); // Prints 'A'Variables whose value cannot be changed (read-only).
- Replacing literal values (e.g., magic numbers) with constants leads to cleaner code.
- Style recommendation: Uppercase, words separated by underscores
final double INTEREST_RATE = 0.069;Defined in java.util, allows reading input from the keyboard.
Imported like so:
import java.util.Scanner;Scanner objects work with System.in, the standard input device.
- Scanner objects retrieve the input formatted as primitive values or strings.
// Create Scanner object
Scanner keyboard = new Scanner(System.in);Scanner class methods to return input as a ...:
nextByte(): BytenextDouble(): DoublenextFloat(): FloatnextInt(): Intnext(): String (until the first space)nextLine(): String (the entire input)nextLong(): LongnextShort(): Short
Note: Remember to close a Scanner object to prevent memory leaks.
- e.g.,
input.close();
Assignment Operator (=): Stores a value in a variable
- Operand on left side must be variable name
- Operand on right side must be a literal or expression/variables that evaluates to compatible type.
More on variables:
- Can only hold one value at a time
- Local variables (variables declared inside a method) do not receive a default value
Java has five arithmetic operators:
| Operator | Meaning |
|---|---|
+ |
Addition |
- |
Subtraction |
* |
Multiplication |
/ |
Division |
% |
Modulus |
- The binary operators must have two operands.
- Division by zero results in an error.
- Each operator must have a left and right operand.
| Operator | Meaning |
|---|---|
- |
Negation |
- Other unary operators and the ternary operator will be discussed later.
Integer division is truncated. Pay attention to types.
e.g.,
double number = 5/2;
System.out.println(number); // Prints "2.0"
number = 5.0/2;
System.out.println(number); // Prints "2.5"| Operator | Associativity | Example | Result |
|---|---|---|---|
| - | Right to left | ||
| * / % | Left to right | 11 | |
| + - | Left to right | 23 |
- When parenthesis are used in an expression, innermost parenthesis are processed first.
- Parenthesis on the same level are processed left-to-right
Java's Math class contains useful methods for complex mathematical operations. e.g.,
Math.pow()Math.sqrt()Math.max()Math.abs()Math.abs()Math.min()Math.log10()- etc...
Predefined
Math.PI
Allow programmer to perform arithmetic and assignment with a single operator.
| Operator | Example |
|---|---|
| += | x+=5 |
| -= | y-=2 |
| *= | z*=10 |
| /= | a /=b |
| %= | c %=3 |
Java performs some conversions between data types automatically if the conversion will not result in the loss of data.
int x;
double y = 2.5;
x = y; // Compile error! (data loss)int x;
short y = 2;
x = y; // This is fineRanking of numeric data types (Java automatically converts smaller to larger, this is called the widening operation):
doublefloatlongintshortbyte
If you need to perform the narrowing conversion, you need to use the cast operator.
TARGET_DATA_TYPE x = (TARGET_DATA_TYPE) number;Java performs promotion automatically when operands are of different data types.
- e.g., if
sumis afloatandcountis anint, the value ofcountis converted to afloatto perform the following calculation:result = sum / count
String is not a primitive data type, it is a class from the Java standard library.
- Notice how
Stringis uppercase because it is a class.
Primitive variables actually contain the value they've been assigned.
Objects (instances of classes) aren't stored in variables. Objects are referenced by variables.
- They store a pointer to the value in memory
- The variable references the object.
- You can create a String object in memory and store its address in a String variable with a simple assignment of a String literal.
- Strings are the only objects that can do this.
// Easy string object creation and assignment
String value = "Hello";
// Standard way to create objects
String value = new String("Hello");Stringobjects are immutable.- Java maintains a String constant pool with String objects can be referenced by other variables.
- e.g., In the following example,
nameandotherNamereference the sameStringobjects.
- e.g., In the following example,
String name = "Hello";
String otherName = "Hello"- Important String library methods:
length()charAt(index)- Remember: Java is zero-indexed.
toLowerCase()toUpperCase()
Scope: The part of a program that has access to a variable's contents.
- Variables declared inside a method are called local variables.
- e.g., Variables declared in the
mainmethod are local variables - A local variable's scope begins at its declaration and ends at the end of the method in which it was declared.
- Thus, you cannot use a variable before it has been declared.
- e.g., Variables declared in the
- You cannot create two local variables with the same name in the same scope.
| Style | Description |
|---|---|
// |
Single line comment. |
/* ... */ |
Block comment. |
/** ... */ |
Javadoc comment. |
Note on Javadoc comments: Allows comments to be documented by the javadoc utility. Can be built into HTMl documentation.
- e.g.,
javadoc <source_code.java>createdindex.htmland several documentation files in the same directory as the input file.
Although the compiler doesn't care about white space, humans do!
Programs should use proper indentation.
- Each block of code should be indented a few spaces from its surrounding block.
Dialog Boxes: Small graphical window that displays a message to the user or requests input.
- A variety of dialog boxes can be displayed using
JOptionPaneclass.JOptionePanecan be imported via:import javax.swing.JOptionPane;
- Two dialog boxes options:
- Message Dialog: Displays a message
- Input Dialog: Prompted user for input
Parse Methods: Method that converts a String to a number.
- Each of the numeric wrapped classes has a parse method
- e.g.,
Integerclass has a method that converts aStringto anint.
- e.g.,
int x = Integer.parseInt("2599");
float y = Float.parseFloat("12.3");
// etc...