-
Notifications
You must be signed in to change notification settings - Fork 1
Java Basics
This week we will learn the basics of Java, a programming language. This guide will walk you through how to write your first Java program as well as how to compile and run your code.
System.out.println() is a Java statement that allows us to display messages on screen. For example:
> public static void main(String[] args) {
> System.out.println("Hello World");
> }
Hello World
Java can be used to interpret basic mathematical expressions.
> 2 + 2
4
> 10 % 5
0
Assume A = 4 and B = 2
Operator | Name | Example |
---|---|---|
+ | Addition | A + B = 6 |
- | Subtraction | A - B = 4 |
* | Multiplication | A * B = 8 |
/ | Division | A / B = 2 |
% | Modulus | A % B = 0 |
++ | Increment | A++ = 5 |
-- | Decrement | A-- = 3 |
In order to process data, computers need it to be organized by specific type. A word is different from a number and to determine the difference between the two we use data types to specify which one is which so we can process it accordingly. Java contains 8 primitive data types, primitive meaning that they are predefined in the language.
Of the 8, 4 deal with integer numbers and 2 deal with decimal numbers.
Integers are whole numbers that can be positive, negative, or zero. The main difference between the 4 integer types is the range of numbers you can use.
Type | Size | Range |
---|---|---|
long | 64-bit | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (-2^63 to 2^63 - 1) |
int | 32-bit | -2,147,483,648 to 2,147,483,647 (-2^31 to 2^31 - 1) |
short | 16-bit | -32,768 to 32,767 (-2^15 to 2^15 - 1) |
byte | 8-bit | -128 to 127 (-2^7 to 2^7 - 1) |
int will probably be your most commonly used integer type. Just be aware that others exist as well.
Double and float are the two data types that express decimal numbers like 3.14159. Again, the only difference between them is the range of numbers.
Type | Size | Range |
---|---|---|
double | 64-bit | 6-7 significant decimal digits |
float | 32-bit | 15 significant decimal digits |
The char data type holds characters. This basically means the alphabet: A-Z, a-z, etc. More specifically chars hold unicode characters which contain basically any and all written symbols.
Last but not least, the boolean data type. This can only hold one of two possible values: true or false. The default value of the data type is set to false.
TBA - figure out an exercise that uses every concept