Minecraft Blogs / Tutorial

Java Tutorial #6 - Natural Numbers

  • 1,603 views, 6 today
  • 3
  • 1
  • 5
SourC00kie's Avatar SourC00kie
Retired Moderator
Level 45 : Master Blob
201
Click here to see Java Programming #5 - Strings

Natural Numbers

Introduction

A number is referred to as natural when it mainly contains digits. Examples are 2, 485, or 182976389. When a natural number doesn't include a sign like these ones, it is referred to as unsigned. That number is also consider positive, that is, above 0. A natural number can have a value under 0. Such a value is referred as a negative. To identify a number as negative, you must type the minus symbol "-" to its left. An example is -246.

In everyday writing, when a number is too long to read, such as 182976389, you can use a special symbol to separate the thousands. In US English, this number is the comma. An example would be 182,976,389. This makes it easy to read. This writing is allowed in various scenarios but you must never use it in your Java programs.

Bytes

A byte is series of 8 bits. It is used to represent small numbers. To declare a variable that would hold a natural number between -128 and 127, you can use the byte data type.

To retrieve the value of a byte variable, apply nextByte() to the Scanner variable. Here is an example:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
byte temperature;

System.out.print("Enter the temperature: ");
temperature = scnr.nextByte();

System.out.print("Temperature: ");
System.out.println(temperature);
}
}

Here is an example of running the program:

Enter the temperature: 32
Temperature: 32

To initialize a byte variable, make sure you assign it a number in the appropriate range. Here is an example:

public class Exercise {
public static void main(String[] args) {
byte temperature = -28;
System.out.print("Temperature: ");
System.out.println(temperature);
}
}

This would produce:

Temperature: -28

The value assigned to a byte variable must in the correct range, otherwise you would receive an error. For example, if you assign a value lower than -128 or higher than 127 to a byte variable, the program would not work.

In the parentheses of System.out.print() or System.out.println(), you can concatenate a string a byte variable. To do this, us the + operator between them. Here is an example:

public class Exercise {
public static void main(String[] args) {
byte temperature = -28;
System.out.println("Temperature: " + temperature);
}
}

To display the v alue of a byte variable using System.out.printf(), use the %d expression. Here is an example:

public class Exercise {
public static void main(String[] args) {
byte temperature = -28;

System.out.printf("Temperature: %d", temperature);
}
}

Regular Integers

The word integer is also used for a natural number. An integer is simply a natural number. Many, if not most, languages provide different data types for various categories of integers. In Java, an integer is variable whose values are between -2,147,483,648 and 2,147,484,647. The value is also said to fit in a 32-bit range. To declare a variable that would hold numbers of that range, use the int keyword.

To retrieve the value a variable of type int, apply nextInt() to the Scanner variable. Here is an example:

import java.util.Scanner;

public class Exercise {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int days;

System.out.print("Enter number of days: ");
days = scnr.nextInt();

System.out.print("Days Passed: ");
System.out.println(days);
}
}

Here is an example of running the program:

Enter number of days: 42
Days Passed: 42

After declaring an int variable, to initialize it, assign a number to it. Here is an example:

public class Exercise {
public static void main(String[] args) {
int days = 468;
System.out.print("Days Passed: ");
System.out.println(days);
}
}

In the parentheses of System.out.print() or System.out.println(), you can concatenate a string an a natural number. To do this, use the + operator. Here is an example:

public class Exercise {
public static void main(String[] args) {
int days = 468;
System.out.print("Days Passed: " + days);
}
}

Instead of a normal decimal number, you can initialize an int variable with a hexadecimal number. The number must be preceded with 0x. Here is an example:

public class Exercise {
public static void main(String[] args) {
int number = 0xA2EE;
System.out.print("Number: " + number);
}
}

This would produce:

Number: 41710

To display the value of an int variable using System.out.printf(), use the %d format to represent a character variable. Here is an example:

public class Exercise {
public static void main(String[] args) {
int number = 41710;
System.out.printf("Number: %d", number);
}
}

To display the number in hexadecimal format, use the %x or %X expression. Here is an example:

public class Exercise {
public static void main(String[] args) {
int number = 41710;
System.out.printf("Number: %x", number);
}
}

This would produce:

Number: a2ee

As you can see, if you use %x, the characters in the result would appear in lowercase. If you want the characters to appear in uppercase, use %X.

Short Integers

An integer is referred to as short if its value is between -32768 and 32767. This number can fit in 16 bits. To declare a variable that would hold numbers in that range, you can use the short data type.

To retrieve a short integer, apply nextShort() to the Scanner variable. Here is an example:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
short numberOfStudents;

System.out.print("Enter number of students: ");
numberofStudents = scnr.nextShort();

System.out.print("Number of Students: ");
System.out.println(numberOfStudents);
}
}

After declaring a short variable, to initialize it, assign the desired number to it. Here is an example:

public class Exercise {
public static void main(String[] args) {
short numberOfStudents = 3428;
System.out.print("Number of Students: ");
System.out.println(numberOfStudents);
}
}

As you may realize, any number that can fit in a short can also fit in an integer. In order words, you can safely use the int keyword to declare a variable that would hold numbers intended for a short. There is no danger. Even if you think of computer memory (because a short uses less space than an int), memory is not expensive anymore.

You can concatenate a string a short variable in the parentheses of System.out.print() or System.out.println(). To do this, use the + operator. Here is an example:

public class Exercise {
public static void main(String[] args) {
short numberOfStudents = 3428;
System.out.print("Number of Students: " + numberOfStudents);
}
}

Just as mentioned for the byte, the value you assigned to a short variable must fit in the allowed range, otherwise you would receive an error when you compile the program.

Instead of a normal decimal integer, you can initialize a short variable with a hexadecimal number. Here is an example:

public class Exercise {
public static void main(String[] args) {
short number = 0x4A8;
System.out.print("Number: " + number);
}
}

To display the v alue of a short variable using System.out.printf(), use the %d expression. Here is an example:

public class Exercise {
public static void main(String[] args) {
short number = 30208;
System.out.printf("Number: %d", + number);
}
}

If you want the result to appear as a hexadecimal number, use %x or %X.

Long Integers

A number that is higher than a regular integer can hold is called a long integer. To support this type of number, the Java language provides the long data type. A long integer is a variable that can hold a very large number that may require 64 bits to be stored accurately. To declare a variable for such a number, use the long keyword.

To retrieve a long value, apply nextLong to the Scanner variable.

After declaring a long variable, to initialize it, assign the desired value to it. Here is an example:

public class Exercise {
public static void main(String[] args) {
long distance = 64564;
System.out.print("Distance: ");
System.out.print(distance);
}
}

This would produce:

Distance: 64564

In the parentheses of System.out.print() or System.out.println(), you can concatenate a string with a long variable. This is done using the + operator. Here is an example:

public class Exercise {
public static void main(String[] args) {
long distance = 64564;
System.out.print("Distance: " + distance);
}
}

You can also initialize a long variable with a hexadecimal number. Here is an example:

public class Exercise {
public static void main(String[] args) {
long number = 0xACE882;
System.out.print("Number: " + number);
}
}

To display the v alue of a long variable using System.out.printf(), use the %d expression. Here is an example:

public class Exercise {
public static void main(String[] args) {
long number = 30208;
System.out.printf("Number: %d", + number);
}
}

If you want the result to appear as a hexadecimal number, use %x or %X.

As you may realize, a number that fits in a short or an int can also fit in a long. This means that you can declare a variable as long but store very small numbers in its memory. Here is an example:

public class Exercise {
public static void main(String[] args) {
long days = 24;
System.out.print("Days: " + days);
}
}

This program works perfectly fine but may result in a significant waste of memory. When you declare a long variable, by default, the compiler "allocates" only enough memory for an integer so there would not be a waste. In some cases, you may want the compiler to use 64 bits for your long variable. Consider the following program:

public class Exercise {
public static void main(String[] args) {
long days = 245885475475;
System.out.print("Days: " + days);
}
}

This would produce:

init:
deps-jar:
Compiling 1 source file to C:ProgramsJavaLessonsExercise1buildclasses
C:ProgramsJavaLessonsExercise1srcexercise1Main.java:24:
integer number too large: 245885475475
long days = 245885475475;
1 error
BUILD FAILED (total time: 0 seconds)

The program would not work because the compiler would have reserved space for an integer but the assigned value needs more room. If you insist on using enough memory for a long integer, when initializing it, on the right side of the value, type L. Here is an example:

public class Exercise {
public static void main(String[] args) {
long days = 245885475475L;
System.out.print("Days: ");
System.out.print(days);
}
}

This time, the program works fine because the compiler was explicitly asked to reserve enough memory.
Tags

Create an account or sign in to comment.

1
04/19/2013 7:30 am
Level 6 : Apprentice Dragon
Vortexy
Vortexy's Avatar
i want to learn but can't even understand a single thing it says :)
1
11/07/2011 6:44 pm
Level 22 : Expert Modder
Xcision
Xcision's Avatar
1
11/07/2011 6:49 pm
Level 45 : Master Blob
SourC00kie
SourC00kie's Avatar
I'm glad that you have found other sources of learning Java. Do you want a medal?
1
11/07/2011 6:51 pm
Level 22 : Expert Modder
Xcision
Xcision's Avatar
No but great tutorial's I learned some new thing's from your's!
1
11/07/2011 11:57 pm
Level 22 : Expert Modder
Xcision
Xcision's Avatar
You should make a website like the newboston's..... You would be great!
Planet Minecraft

Website

© 2010 - 2024
www.planetminecraft.com

Welcome