Basic Syntax and Programming Fundamentals
Basic Syntax and Programming Fundamentals
KNOW YOUR
PROGRAMMING
ENVIRONMENT
" " Opening and closing Enclosing a string (i.e., sequence of characters).
quotation marks
; Semicolon Marks the end of a statement.
INTEGER LITERALS
- Integer literals come in different formats: decimal (base10),
hexadecimal (base 16), and octal (base 8). In using integer
literals, special notation should be followed.
- By default, an integer literal is a decimal integer number. The
decimal number is written as it is. To denote an octal integer
literal, use a leading 0 (zero), and to denote a hexadecimal
integer literal, use a leading 0x or 0X (zero X).
- Example: int i = 123;
- System.out.println(156);
LITERALS
FLOATING – POINT LITERALS
- Floating-point literals are written with a decimal point. By
default, a floating-point literal is treated as a double type
value. For example, 5.0 is considered a double value, not a
float value. You can make a number a float by appending the
letter f or F.
- Floating point literals can be also expressed in standard or
scientific notations. For example, 583.45 is in standard
notation, while 5.8345e2 is in scientific notation.
- A float value has 7 to 8 number of significant digits and a
double value has 15 to 17 number of significant digits.
- Example: System.out.println(15.6);
- System.out.println(9.6F);
LITERALS
BOOLEAN LITERALS
- Boolean literals have only two values, true or false.
- Example: boolean b1 = true;
CHARACTER LITERALS
- Stores single characters, such as 'a' or 'B'. Char values are
surrounded by single quotes.
- Example: char myLetter = 'D';
STRING LITERALS
- Stores text, such as "Hello". String values are surrounded by
double quotes.
- Example: String s = "hello";
- System.out.println("This is some text");
DATA TYPES
- Data types are used to specify the type of data or value a
variable would hold.
There are eight primitive data types in Java:
NUMBERS
Primitive number types are divided into two groups:
Integer types stores whole numbers, positive or
negative (such as 123 or -456), without decimals. Valid
types are byte, short, int and long. Which type you
should use, depends on the numeric value.
Floating point types represents numbers with a
fractional part, containing one or more decimals. There
are two types: float and double.
INTEGER TYPES
Byte
The byte data type can store whole numbers from -128 to 127.
This can be used instead of int or other integer types to save
memory when you are certain that the value will be within -128
and 127:
Example: byte myNum = 100;
System.out.println(myNum);
Short
The short data type can store whole numbers from -32768 to
32767:
Example: short myNum = 5000;
System.out.println(myNum)
INTEGER TYPES
Int
The int data type can store whole numbers from -2147483648 to
2147483647. In general, and in our tutorial, the int data type is the
preferred data type when we create variables with a numeric
value.
Example: int myNum = 100000;
System.out.println(myNum);
Long
The long data type can store whole numbers from -
9223372036854775808 to 9223372036854775807. This is used
when int is not large enough to store the value. Note that you
should end the value with an "L":
Example: long myNum = 15000000000L;
System.out.println(myNum);
FLOATING POINT
TYPES
You should use a floating point type whenever you need a number
with a decimal, such as 9.99 or 3.14515.
The float and double data types can store fractional numbers.
Note that you should end the value with an "f" for floats and "d" for
doubles:
Float Example
float myNum = 5.75f;
System.out.println(myNum);
Double Example
double myNum = 19.99d;
System.out.println(myNum);
JAVA BOOLEAN DATA
TYPES
Boolean Types
Very often in programming, you will need a data type that can only
have one of two values, like:
• YES / NO
• ON / OFF
• TRUE / FALSE
For this, Java has a boolean data type, which can only take the
values true or false:
Example:
boolean isJavaFun = true;
boolean isFishTasty = false;
System.out.println(isJavaFun); // Outputs true
System.out.println(isFishTasty); // Outputs false
JAVA CHARACTERS
Characters (Char)
The char data type is used to store a single character. The
character must be surrounded by single quotes, like 'A' or 'c':
Example:
char myGrade = 'B';
System.out.println(myGrade);