Data Types in Java

Table of Contents

Introduction

In computer science, data is grouped according to type. This chapter covers the naming conventions used for data types in Java.

Java is a statically typed programming language, meaning that what kind of data it is is as important as the data itself. In Java, when creating an array, declaring a variable, or making a function, you must declare the type before its name.

Numbers

Integer

Integers in computer science refer to whole numbers.

100 - an integer

How to declare an integer in Java:

int x = 100;

Double

Doubles refer to numbers fixed to a point. Example: Decimal numbers

1.50 - a double

How to declare a double in Java:

double y = 1.50;

Operands

Operands are symbols signifying a specific mathematical operation to be performed.

In this example, the operand + tells us to add the numbers 1 and 2 together:

1 + 2

Some common operands:

+ - addition of numbers, and concatenation of Strings

- - subtraction of numbers

/ - division of numbers

* - multiplication of numbers

= - assigns a value. The value item on the left side becomes the value of the item on the right side.

== - strict equality. The value of the item on the left must be the same as the value of the item on the right side.

% - pronounced "modulo". Determines the remainder of dividing two numbers.

Booleans

Booleans refer to items that evaluate to either true or false.

How to declare a boolean in Java:

boolean sorted = false;

Characters

A character refers to a single symbol between ''. Notice the single quotation marks.

Examples of characters:

'a'

'@'

'1'

How to declare a character in Java:

char letter = 'a';

Strings

A String refers to a series of characters between "". It is most frequently used for holding text.

Examples of Strings:

"abcde"

"The quick brown fox jumps over the lazy brown dog"

"12345678"

How to declare a string in Java:

String talk = "blah blah blah";

Arrays

2D Arrays

External Resources

results matching ""

    No results matching ""