📕
Knowledge
  • Knowledge Repository
  • Java
    • Intro
    • Persistence
      • Java Persistence API (JPA)
        • Entity Manager
        • Transaction Management
        • Relationship Mapping
      • Java Transaction API (JTA)
    • Resources
  • Angular
    • Intro
    • Overview
    • CLI Commands
    • Directives
  • Tools
    • IDEs
      • IntelliJ
        • Shortcuts
  • Book Notes
    • Intro
    • Java A Beginner's Guide
      • Chapter 1. Java Fundamentals
      • Chapter 2. Data Types and Operators
      • Chapter 3. Program Control Statements
      • Chapter 4. Introducing Classes, Objects, and Methods
      • Chapter 5. More Data Types & Operators
      • Chapter 6. Closer Look At Methods and Classes
      • Chapter 7. Inheritance
      • Chapter 8. Packages & Interfaces
      • Chapter 9. Exception Handling
      • Chapter 10. Using I/O
    • Data Pipelines With Airflow
      • Chapter 1. Meet Airflow
      • Chapter 2. Anatomy Of a DAG
  • Course Notes
    • Intro
    • Spring: TDD With JUnit
Powered by GitBook
On this page
  • The Importance of Data Types in Java
  • Java Primitive's
  • Integers
  • Floating-point Types
  • Char
  • Boolean Type
  • Literals
  • Hexadecimal & Octal literals
  • Escape Sequences
  • String Literals
  • Operators
  • Arithmetic
  • Relational
  • Logical

Was this helpful?

  1. Book Notes
  2. Java A Beginner's Guide

Chapter 2. Data Types and Operators

The Importance of Data Types in Java

Java is a strongly typed language, which under the hood means that all operations are type-checked by the compiler for type compatibility. A string value cannot be stored in a variable of type Integer, this would be considered an Illegal operation. Strong type checking helps prevent these sort of errors, especially in large applications.

Java Primitive's

There are two categories of built-in data types in Java.

  • Object-orientated

  • Non object-orientated

The non objected-orientated types are the primitives, and there are 8 of them. The term primitive is used to indicate that these types are not objects.

Type

Meaning

Width in Bits

boolean

true/false

1

byte

8-bit integer

8

char

Character

16

double

Double-precision floating point

64

float

Single-precision floating point

32

int

Integer

32

long

Long integer

64

short

Short integer

16

Integers

There are four integer types, byte, short, int and long. All of these integer types can be assigned either positive or negative values. Java does not support unsigned (positive-only) integers like some other languages do.

For most cases you'll typically use an int.

The smallest integer type is a byte, which is typically useful for when working with raw binary data.

Floating-point Types

There are two floating-point types to represent fractional numbers. 1. float - single precision - 32 bits wide 2. double - double precision - 64 bits wide

Double is the most commonly used type and many of the Java class library math functions will return a double.

Char

In Java a char is an unsigned 16-bit type that can be used to represent the characters found in all human langages. This is Unicode. This is why a Java char is 16-bit and not 8-bit like a lot of other programming languages that use ASCII.

ASCII character set is a subset of Unicode and as such can still be used in Java.

As char is an unsigned type, it is possible to perform arithmetic functions on a variable. For example:

char letter = 'X';
letter++
System.out.println(letter);
Y

Boolean Type

The boolean type can be one of two values, true or false.

A boolean variable can control a conditional if statement without explicit code. For example:

boolean isAllowed == true;

if (isAllowed) { .... this is OK

if (isAllowed == true) { ....  this will compile but it is not neccessary

A relational operator will return a boolean value, and does not require explicit code either. For example:

return 5 > 2;

Literals

Literals refer to fixed values, for example 100 is a literal and are also known as constants. Literals can be any of the primitive types. char literals are specified with single quotes, whereas string literals with double quotes.

Long and float primitives can also be specified by adding a character to the end of value. For example:

long longNumber = 12345678910L
float floatNumber = 1234.56F

To make integers and floating-point literals easier to read, you can embed underscores which are removed at compile time.

int number = 123_456_789
==> 123456768

This is typically useful for numbers such as customer IDs and parts numbers.

Hexadecimal & Octal literals

Number systems such as octal and hexadecimal can be represented in Java.

The system based on 8 is octal and base 16 system is hexadecimal. Octal uses numbers 0 to 7, and hexadecimal uses digits 0 to 9, and letters A to F, which stand for 10, 11, 12, 13, 14, 15.

int hexNumber = 0xFF; // 255 in decimal
int octNumber = 011; // 9 in decimal

Escape Sequences

Some characters in Java can pose an issue when used in certain situations. For example, single and double quotes are used to enclose strings, but what if you need to display a quote mark? That's where escape sequences can help. Also referred as backlash character constants.

Escape Sequence

Description

\'

Single quote

\"

Double quote

\

Backslash

\r

Carriage return ↵

\n

New line

\f

Form feed

\t

Horizontal tab

\b

Backspace

\ddd

Octal constant (ddd is the octal value)

\uxxxx

Hexadecimal contstant (xxxx is the value)

If I wanted to assign a backslash to a variable, it would be the following char backSlash = '\';

String Literals

A string literal is a set of characters enclosed by double quotes. (single quotes will not work).

A string literal can contain one or more of the escaoe sequences from above.

A string literal that contains a single character is not the same as a single letter of type char.

Operators

Java has four general classes of operators: 1. Arithmetic 2. Bitwise 3. Relational 4. Logical

Arithmetic

Operator

Description

+

Addition

-

Subtraction

*

Multiplication

/

Division

%

Modulus

++

Increment

--

Decrement

Note, when using an operator on int values, any remainder will be truncated.

Increment and decrement operators can also be used to control when the operation takes place.

For example: x = 10; y = ++x; y will be set to 11. __

x = 10; y = x++; y will be set to 10.

In both cases, x is still set to 11.

Relational

Operator

Description

==

Equal to

!=

Not equal to

>

Greater than

<

Less than

>=

Greater than or equal to

<=

less than or equal to

Logical

Operator

Description

&

AND

|

OR

^

XOR (exclusive OR)

||

Short-circuit OR

&&

Short-circuit AND

!

NOT

XOR represents the inequality function, i.e., the output is true if the inputs are not alike otherwise the output is false. A way to remember XOR is "must have one or the other but not both".

Example from Baeldung: Car car = Car.dieselAndManualCar(); boolean dieselXorManual = (car.isDiesel() && !car.isManual()) || (!car.isDiesel() && car.isManual());

Above can be reduced to: Car car = Car.dieselAndManualCar(); boolean dieselXorManual = car.isDiesel() ^ car.isManual();

The outcome of relational and logical operators is always a boolean value.

The only difference between the short-circuit and the normal operator is that normal will always evaluate each operand, whereas short-circuit will evaluate the second operand only when necessary. This saves time and is more efficient.

PreviousChapter 1. Java FundamentalsNextChapter 3. Program Control Statements

Last updated 4 years ago

Was this helpful?