Declaring Variable and Literal
Java supports some basic programming elements, such as data types, keywords, literals and variables. Keywords are the reserved words for Java programming language, which cannot be used as names for variables, class or methods.
Various Data Types
The data stored on memory of the computer can be of different types. For example, a person’s age is stored as a numeric value and an address is stored as alphanumeric characters. Java is a strictly typed language, which means that Java gives importance to type checking. Expressions and variables in Java can be of different types, such as int, char, or string. Data Types in Java are divided into two categories as stated below:
- Primitive or the simple data types
- Abstract or the derived data types
Primitive Data Types
Built in data types in Java are known as the primitive data types. A primitive data type can store exactly one value of its declared type at a time. primitive-type instance variable are initialized by default – variables of types byte, char, short, int, long, float and double are initialized to zero and variables of type Boolean are initialized to false. There are eight primitive data types in Java, which are further grouped into the following categories:
- Integer type: this type of variables can store whole number values. The size of the values of the variable depends upon the chosen integer data type. The four integer data types are:
- Byte
- Short
- Int
- Long
- Floating point type: these data type can store fractional numbers. The two types of floating point are:
· Float
· Double
- Boolean type: these data type can store only the true and false values. Boolean data type is required when a condition has to be checked. The true or false value of the expression or the condition determines further execution of the Java program.
- Character type: these data type can only store symbols, such as letter and numbers. In character type, there is only one data type, char.
Abstract data types
The abstract data types include the data types derived from the primitive data types and have more functions than primitive data types. For example, String is an abstract data type that can store letters, digits and other characters, such as /,(),:, and #. You can not perform calculations on a variable of the String data type even if the data stored in it has digits. However, String provides methods for concatenating two strings, searching for one string within another and extracting a part of a string.
Keywords in Java
Keywords are reserved words for a language which expresses the language features. Keywords cannot be used for naming purpose of variables, constants, or classes. Java is a case sensitive language and the keywords should be written in lowercase only. The keywords with all or some letters in uppercase can be treated as variable but that should be avoided. The following are some examples of keywords in Java.
Abstract, case, double , finally, if, int, new, public, this, try, Boolean, catch, continue, else, float, implements, interface, package, return, super, throw, void, break , char, default, extends, import, long, private, short, switch, throws, byte, class, do, final, goto, protected, static, and while.
NOTE: There are some others that are not mentioned here.
Defining Variables and Literals
A variable is used to store and manipulate data or values in a program. A variable is the name that refers to a memory location where some data value is stored. It is possible to assign different values to a variable during program execution. Java allocates memory to each variable that you use in your program.
All variable used in a program must first be declared, for example, if the variable age is to be used for storing an integer value, the variable age must be declared and it should be of type int.
Naming Conventions for Variables
The naming conventions for variables in Java include the following:
- The names of variables needs to be short, meaningful and without any embedded space or symbol such as ?, !,#,@,%,[],:” and /.
- A variable name must begin with a letter, an underscore (_) or the dollar symbol ($) which can be followed by a sequence of letters or digits.
- Variable names must be unique.
- A variable name should not consist of a keyword
- A variable name in Java is case sensitive. For example, age is not the same as Age.
Types of Variable
A variable scope is the area in a program where a variable can be access. The various types of variable on the basis of the variable scope in Java include:
Class variable: class variables are accessible within a class and its objects. They are declared inside the class before they are being used.
Instance variables: they are declared inside a class and are created when the class is instantiated.
Local variables: local variables are declared within a method and their scope is limited within the block of codes in which they are declared. They can not be accessible outside the method.
Static variables: they are allocated memory only once but are globally accessible to all instances of a class.
Declaring Variables
When a variable is declared, the compiler is informed about the variable name, data type and scope of the variable. A variable must first be declared before it can be accessed. Variable can be declared as stated below.
<type> <variablename>; (An example of declaring a single variable with give type)
Or
<type> <variableAname, variableBname, variableCname, variableDname> (An example of declaring multiple variables of a given type)
You can choose to first declare a variable before assigning values to it or you can choose to assign values to it while declaring the variable. The examples above explained how a variable can just be declared without assigning values to it. The below example shows how to assign values during a variable declaration.
<type> <variablename>=<value>
Int number1, number2; (Declaration of variables number1 and number2)
Number1 = 10;
Number2 = 20; (Assigning values to the already declared variable)
Literals in Java
Literals are the values stored in an already declared variable. Literals can contain a sequence of characters, such as alphabets, digits and any other symbol that represents the value to be stored. The various types of literals in Java include the following:
Integer Literals: they are numeric type values. E.g number1 = 58; and number2 = 40;
Floating point literals: they are numerical values with fractional part. For example, number1 = 6.2 is a floating point literal.
Character literals: they are represented in single quotation marks. For example, x= ‘k’ is a character literal.
String literals: they are enclosed in double quotation marks. For example, name = “Gino” is a string literal.
Boolean literals: they are literals having values true or false. For example, x = false is a Boolean literal.
Manipulating Variables
Operators are used to manipulate data and variables in Java, operators accept one or more operands or arguments and produce an output. The operators used to manipulate variables are the assignment and arithmetic operators.
Assignment Operators
The assignment operators (=) are used to assign values to a variable. The following syntax is used for the assignment operator:
Op1 = op2;
In the above syntax, the assignment operator (=) assigns the value of the right operand op2 to the left operand op1.
Arithmetic Operators
Arithmetic operators are used to compute mathematical expressions. The following lists the various arithmetic operators.
Operators Operations
+ Adds two operands
- Subtracts one operand from another
* Multiplies two operands
/ Divides two operands
% Calculate the modulus
++ Increments a variable
-- Decrement a variable
In Java, basic mathematical operations, such as addition, subtraction, multiplication and division can be performed using +,-,*, and / operators.
The addition operator (+) is used to add two integers and concatenate two string values. For example, you have two numbers, 10 and 23. to add these numbers, you can use the following statement:
10 + 23
In the preceding statement, you use the + operator to concatenate the two values to produce a result 33.
You can also use the + operator to concatenate two string values. For example Gino + wine, will produce the result Ginowine.
No comments:
Post a Comment