1 - Types, Variables, Operators
1.0 Table of Contents
- 1.1 Why learn Java?
- 1.2 Fundamentals of the Computer
- 1.3 First Program
- 1.4 Types
- 1.5 Variables
- 1.6 Operators
- 1.7 Assignment 1 - Falling Object
1.1 Why learn Java?
- Widely Used:
- web development
- mobile app development
- enterprise software
- scientific computing
- etc
- ample resources
- libraries
- frameworks
- tools for learning and building Java application
- platform independence
- write code once
- run on any operating system that supports Java
- Android applications
- opportunities to develop for the Android platform
- emphasis on OOP concepts
- object-oriented programming
- fundamental to modern software development
- reliability
- memory management
- exception handling
- help create stable and secure applications.
- good scalability
- build applications small or large
- enterprise-level systems
- a versatile language

1.2 Fundamentals of the Computer
1.2.1 Computer Logic
1.2.2 CPU Instructions
z=x+y;
LOAD x
ADD y
STORE z
1.2.3 Programming Languages
- High-level languages
- Java
- Python
- C++
- C#
- etc
- wasier to understand than assembly
- must be translate to machine code to run
1.2.4 Compile Java
1.3 First Program
go to repl.it and create a new Java project
1.3.1 HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
1.3.2 Program Structure
class CLASSNAME{
public static void main(String[] args) {
// code goes here
}
}
1.3.3 Output to the Console
System.out.println("some string");
1.4 Types
- Java is a statically typed language
- variables must be declared before they can be used
1.4.1 Primitive Data Types
| Type | Description | Size | Range |
|---|---|---|---|
| byte | 8-bit signed integer | 1 byte | -128 to 127 |
| short | 16-bit signed integer | 2 bytes | -32,768 to 32,767 |
| int | 32-bit signed integer | 4 bytes | -2,147,483,648 to 2,147,483,647 |
| long | 64-bit signed integer | 8 bytes | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
| float | 32-bit floating point | 4 bytes | 3.4e-038 to 3.4e+038 |
| double | 64-bit floating point | 8 bytes | 1.7e-308 to 1.7e+308 |
| boolean | true or false | 1 bit | true or false |
| char | 16-bit unicode character | 2 bytes | 0 to 65,535 |
on top of these, Java have support for a character string type, called String
1.4.2 Default Values
| Type | Default Value |
|---|---|
| byte | 0 |
| short | 0 |
| int | 0 |
| long | 0L |
| float | 0.0f |
| double | 0.0d |
| boolean | false |
| char | ‘\u0000’ |
| String | null |
1.5 Variables
variables are named location that stores a value of one particular type
1.5.1 Variable Declaration
type name;
Example:
int x;
String foo;
1.5.2 Variable Assignment
name = value;
Example:
int x;
x = 5;
String foo;
foo = "bar";
1.5.3 Variable Initialization
type name = value;
Example:
int x = 5;
String foo = "bar";
1.5.4 HelloWorld2.java
public class HelloWorld2 {
public static void main(String[] args) {
String message = "Hello World";
System.out.println(message);
message = "Hello World 2";
System.out.println(message);
}
}
1.6 Operators
symbols that perform operations on operands operands are variables and values that operators act on
1.6.1 Arithmetic Operators
| Operator | Description | Example |
|---|---|---|
| + | addition | x + y |
| - | subtraction | x - y |
| * | multiplication | x * y |
| / | division | x / y |
| % | modulus | x % y |
| ++ | increment | x++ |
| – | decrement | x– |
1.6.2 Assignment Operators
| Operator | Description | Example |
|---|---|---|
| = | assign | x = y |
| += | add and assign | x += y |
| -= | subtract and assign | x -= y |
| *= | multiply and assign | x *= y |
| /= | divide and assign | x /= y |
| %= | modulus and assign | x %= y |
| … | … | … |
1.6.3 Comparison Operators
| Operator | Description | Example |
|---|---|---|
| == | equal to | x == y |
| != | not equal to | x != y |
| > | greater than | x > y |
| < | less than | x < y |
| >= | greater than or equal to | x >= y |
| <= | less than or equal to | x <= y |
1.6.4 Logical Operators
| Operator | Description | Example |
|---|---|---|
| && | logical and | x > 0 && x < 10 |
| || | logical or | x < 0 || x > 10 |
| ! | logical not | !(x == y) |
1.6.5 Bitwise Operators
| Operator | Description | Example |
|---|---|---|
| & | bitwise and | x & y |
| | | bitwise or | x | y |
| ^ | bitwise xor | x ^ y |
| ~ | bitwise compliment | ~x |
| « | left shift | x « 2 |
| » | right shift | x » 2 |
| »> | unsigned right shift | x »> 2 |
| … | … | … |
1.6.6 Order of Operations
The standard order of operations in math
- Parentheses
- Multiplication and Division
- Addition and Subtraction
1.6.7 DoMath.java
class DoMath{
public static void main(String[] arguments){
double score = 1.0 + 2.0 * 3.0;
SSystem.out.println(score);
score = score / 2.0;
System.out.println(score);
}
}
1.6.8 DoMath2.java
class DoMath2{
public static void main(String[] arguments){
double score = 1.0 + 2.0 * 3.0;
System.out.println(score);
double score2 = score / 2.0;
System.out.println(score);
System.out.println(score2);
}
}
1.6.9 String Concatenation
you can use the + operator to concatenate strings
String message = "Hello" + " " + "World";
message += "!";
//message = "Hello World!"
1.7 Assignment 1 - Falling Object
Compute the position of a falling object and print it
- use the equation
position = 0.5 * g * time * time + initialVelocity * time + initialPosition- where
gis the gravity constant-9.8 m/s^2 positionis in meterstimeis in secondsinitialVelocityis in meters per secondinitialPositionis in meters
if time is 10 seconds, initialVelocity is 0, and initialPosition is 0, then the position should be -490.0m
class FallingObject{
public static void main(String[] args){
/*
* Steps to solve the problem
* 1. Declare variables
* 2. Assign values to variables
* 3. Compute the position
* 4. Print the result
*/
}
}