Java String Concatenation

String Concatenation

The + operator can be used between strings to combine them. This is called concatenation:

Example

String firstName = "John";
String lastName = "Doe";
System.out.println(firstName + " " + lastName);

 

Continue reading Java String Concatenation

Java Strings

Java Strings

Strings are used for storing text.

A String variable contains a collection of characters surrounded by double quotes:

Example

Create a variable of type String and assign it a value:

String greeting = "Hello";

 

Continue reading Java Strings

Java Operators

Java Operators

Operators are used to perform operations on variables and values.

In the example below, we use the + operator to add together two values:

Example

int x = 100 + 50;

 

Although the + operator is often used to add together two values, like in the example above, it can also be used to add together a variable and a value, or a variable and another variable:

Example

int sum1 = 100 + 50;        // 150 (100 + 50)
int sum2 = sum1 + 250;      // 400 (150 + 250)
int sum3 = sum2 + sum2;     // 800 (400 + 400)

 

Java divides the operators into the following groups:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Bitwise operators

Continue reading Java Operators

Java Type Casting

Java Type Casting

Type casting is when you assign a value of one primitive data type to another type.

In Java, there are two types of casting:

  • Widening Casting (automatically) – converting a smaller type to a larger type size
    byte -> short -> char -> int -> long -> float -> double
  • Narrowing Casting (manually) – converting a larger type to a smaller size type
    double -> float -> long -> int -> char -> short -> byte

Continue reading Java Type Casting

Java Non-Primitive Data Types

Non-Primitive Data Types

Non-primitive data types are called reference types because they refer to objects.

The main differences between primitive and non-primitive data types are:

  • Primitive types in Java are predefined and built into the language, while non-primitive types are created by the programmer (except for String).
  • Non-primitive types can be used to call methods to perform certain operations, whereas primitive types cannot.
  • Primitive types start with a lowercase letter (like int), while non-primitive types typically starts with an uppercase letter (like String).
  • Primitive types always hold a value, whereas non-primitive types can be null.

Examples of non-primitive types are Strings, Arrays, Classes etc. You will learn more about these in a later article. Continue reading Java Non-Primitive Data Types

Java Data Types Example

Real-Life Example

Here’s a real-life example of using different data types, to calculate and output the total cost of a number of items:

Example

// Create variables of different data types
int items = 50;
float costPerItem = 9.99f;
float totalCost = items * costPerItem;
char currency = '$';

// Print variables
System.out.println("Number of items: " + items);
System.out.println("Cost per item: " + costPerItem + currency);
System.out.println("Total cost = " + totalCost + currency);

 

Continue reading Java Data Types Example

Java Characters

Characters

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);

 

Alternatively, if you are familiar with ASCII values, you can use those to display certain characters: Continue reading Java Characters

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: Continue reading Java Boolean Data Types

Java Numbers

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.

Even though there are many numeric types in Java, the most used for numbers are int (for whole numbers) and double (for floating point numbers). However, we will describe them all as you continue to read.


Continue reading Java Numbers

Java Data Types

Java Data Types

As explained in the previous chapter, a variable in Java must be a specified data type:

Example

int myNum = 5;               // Integer (whole number)
float myFloatNum = 5.99f;    // Floating point number
char myLetter = 'D';         // Character
boolean myBool = true;       // Boolean
String myText = "Hello";     // String

 

Continue reading Java Data Types