Week 2

Table of Contents

Primitive Data Types in Java
Operators in Java
Classes and the Math Class
Packages in Java
Variable Scope in Java
Looping Structures in Java
Conditional Structures in Java

Primitive Data Types in Java

The following table shows the primitive data types in Java.  

Data Type Size Description
byte 8-bit (1 byte) Stores numbers with a range ot -128 to 127
short 16-bit (2 byte) Integer type with a range of -32,768 to 32,767
int 32-bit (4 byte) Integer type with a range of -2,147,483,648 to 2,147,483,647
long 64-bit (8 byte) Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float 32-bit (4 byte) Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits
double 64-bit (8 byte) Stores fractional numbers. Sufficient for storing 15 decimal digits
boolean 1 bit Stores true or false values (often can also be referred to 1 or 0)
char 16-bit Unicode (2 byte) Stores a single character/letter or ASCII values

Note that String is not a primitive data type, but a string is made up of an array of chars

Reference: Java Data Types (w3schools.com) 

According to W3Schools:

"The String type is so much used and integrated in Java, that some call it "the special ninth type".

String in Java is actually a non-primitive data type, because it refers to an object. The String object has methods that are used to perform certain operations on strings. Don't worry if you don't understand the term "object" just yet. We will learn more about strings and objects later."

Signed vs. Unsigned

Some documentation will refer to numeric data types as being signed or unsigned.  The word sign simply means whether the number is preceded by a "+" or "-".  The range of values of an unsigned data type are simply shifted to the right.

For example:  A signed short can range from -32,768 to +32,767.  An unsigned short can range from 0 to 65,534.

Constants

Constants are variables that are assigned a value at declaration, and the value cannot be changed later without throwing an exception.

In C languages, such as C, C++, or C# constants are defined using the const keyword.  Since const is a keyword used elsewhere in java, java using the keyword final to define static constants.

Declaring constants in various languages

Language Keyword Example
C typeConstant int decimalConstant = 42;
C++ const const int myNum = 15;
C# const const int myNum = 15;
Java final final int myNum = 15;
JavaScript const const x = 5;
Python does not support constants, use naming conventions and style guides to assist developers
Swift let let y = 4;
Kotlin val val y = 3;