Java

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.

Basic Things of Implementing JavaScript to Web Pages

Fadıl

JavaScript can be typed in any text editor like notepad and can be stored separately with file extension.js. Usually, it is stored as a part of the HTML document. JavaScript codes are included in HTML documents by using the <script> tag.

The script tag is a non-empty tag and should be closed using the </script> tag. The script tag takes in two attributes namely language and src. The language attribute specifies the name of the scripting language used like JavaScript and VBScript. The src attribute specifies the name of the source file where the script is stored. The src is used only when the scripts are stored in a separate file. <script language = “ “ src = “ “ >