Java Basics – Numbers and Strings using BlueJ

JAVA BASICS

Introduction

In Java, all values have types.  Some of the most basic types are numbers and strings. Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects. There are different types of numbers, integers, double and float. Here we explore some of the uses of numbers and strings.

Numbers

A number in Java can be an integer or a floating-point number. Floating point are known as real numbers in mathematics. They are called floating points because they are stored in something similar to scientific notation. Floating points float from left to right. 

E.g. 0.001 = 1 * 10^-3

Computers do the same thing using the binary system and not decimals. There are two types of floating points in Java

  1. Float
  2. Double

There is not much difference between these two types except that double has twice as much space in memory and thus stores bigger and more precise numbers. 

Strings

In order to make a value a string, it has to be enclosed in double quotes. Some languages like python allow both single and double quotes, however in Java you can only use double quotes. 

If an integer is enclosed in double quotes as below, then it is treated as a string. However, if it is not, then it is treated as an integer. You would not be able to tell the difference in output in print statements, however they are fundamentally different and are able to do different things in a program.

/**The main function initiates the execution of this program.*/


   public static void main()

   {

       System.out.println("Brown skin girl"); // Prints a string
       System.out.println("2020");           // Prints  an integer
       System.out.println("0.001");        //Prints a float or double       

  }

Output in numbers and strings

The first print statement below, will print out a string as 2 + 3 while the second print statement will be evaluated, and the sum will be given as output top get a value of 5.

/**The main function initiates the execution of this program.*/

public static void main()
{
System.out.println("2 + 3");
System.out.println(2 +  3);          

}

Order of evaluation of Operations

PMDAS

Parenthesis, Multiplication and Division, Addition and Subtraction

The order of precedence for operators in Java is as above (from left to right).

    /**The main function initiates the execution of this program.*/

 public static void main()

{

        System.out.println("2 + 4"); 
System.out.println(2 +  4 + 5); 
System.out.println(2 +  3 * 5)
System.out.println((2 +  3) * 5);

}

In the first print statement, output will be treated as a string because of the double quotes thus output will be  2 + 4.

In the second print statement, the sum will be evaluated an output is 11.

In the third print statement, the output will be 17 because multiplication takes precedence. 3 by 5 is evaluated first then 2 is added. 

In the fourth statement, the output will be 25 because we evaluate what is in the parenthesis first, then the answer is multiplied by 5 to get 25.

Conclusion

This is a basic intro to numbers and strings.