Introduction to Java : Data Types and Operators Part-4

Fadıl

Operators

Java also provides several operators that you can use to manipulate your variables.

Mathematical Operators

As in all languages, Java allows you to perform math functions. The following table outlines the available functions:

OperatorDescriptionExample
+Performs additionint x = 1; int y = 2; int z = x + y // z = 3
–Performs subtractionint x = 2; int y = 1; int z = x – y // z = 1
\ *Performs multiplicationint x = 2; int y = 2; int z = x * y // z = 4
/Performs divisionint x = 4; int y = 2; int z = x / y // z = 2
%Gets the remainder (modulus)int x = 6; int y = 4; int z = x % y // z = 2

One thing to remember about mathematics in Java is that since variables are typed, you should be careful to match types or expect automatic casting! In other words, Java will return the result in a type that fits in the result regardless of what was used in the equation.

Introduction to Java : Data Types and Operators Part-3

Fadıl

Strings

As you may have noticed, in Java, there is no variable type called string. However, although Java does not have a built-in String type, it does provide you will a predefined class called String that you can use instead.

We’ll talk more about classes and using them in later sections. However, it would be nice to at least introduce the String class here so we can use it in the meantime.

Introduction to Java : Data Types and Operators Part-2

Fadıl

Data Types

Java is a “strongly typed language”. That means that type of every variable must be declared.

Consider the following simple variable assignments:

     boolean isASwingDancer = true;
     int age = 30;

In other words, Java requires that if a variable is meant to hold a boolean, it is declared as such. The same goes for ints and every other data type.

Once you declare a variable, it can never be changed to hold another type of value.

Introduction to Java : Data Types and Operators Part-1

Fadıl

Comments

It is often the case that you want to include comments within your Java code. Comments are little bits of documentation embedded in the actual code that is invisible to the Java compiler but which can be read by a person reviewing your code.

Comments help to document what it is that your code does and how someone should understand the design if they want to modify something. Comments are essential if someone else is going to work with you or take from where you’ve left off.

What’s A Netmask?

Fadıl

And Why Do I Need One?

When you set up a small network that interconnects to another network, the assignment of IP addresses and the “netmask” are very important. A lot of people get confused about this part, so here’s a short explanation of what’s going on.


IP Addresses
On a TCP/IP network, which is what you are building, every machine has a unique IP address, consisting of a total of 32-bits. The address is usually expressed as four decimal numbers separated by dots, where each number represents 8 bits of address. Each number therefore ranges from 0 to 255. (Note: the world is running out of IP addresses, so a new address scheme called IPng is being developed that uses six numbers instead of four. It is not yet actually in use.)