Character Set

  • Java uses the Unicode character set. Unicode defines a fully international character set that can represent all of the characters found in all human languages and writing systems around the world such as English, Arabic, Chinese, Hindi, Regional languages etc.
  • Since there are a large number of languages in the world, therefore a large character set is required and 8 bits, are not sufficient. Therefore, this 16-bit character set fulfills the need. Thus, in Java char is a 16-bit type.

Data Types

  • In most of the other programming languages, the format and size of primitive data types depend on the platform/operating system or processor on which a code is running. But in Java programming language, the size and format of its primitive data types are specified. So we need not worry about the system dependencies while using Java primitive data types. This makes Java programs easily portable on any machine architecture.
Primitive Data Types
Major Category

Data Type

Default Value

Memory Size(in bytes/bits)
Range Example

Description 

Unsigned Signed
Characters Char   \u0000 2/16 
\u0000 to \uFFFF
‘P’, ‘3’, ‘n’ A single character
Boolean Boolean    False 1 bit used
in 32 bit
integer
N/A

True/1 or false/0 A boolean value (0 or 1)

Numeric


Byte  0  1/8 0-255 -128 to 127 23, 102 Byte-length integer 
Short  0  2/16 0-65535 -32768 to 32767 10, 365,855 Short integer
Int  0  4/32 0-4294967295 -2147483648 to 2147483647 36, 457, 45213 Integer 
Long  0  8/64   -9223372036854775808 to
9223372036854775807
58L, -659L Long integer 
Real
numbers/IEEE 754
floating point numbers

 
Float

0.0 4/32    +/-1.4E-45 to +/-
3.4028235E+38,
+/-infinity, +/-0, NAN
54.32f, -65.563f, Single-precision
floating point
Double

0.0 8/64    +/-4.9E-324 to
+/-
1.7976931348623157E+308,
+/-infinity, +/-0, NAN
587.25, -21.05,
685.6E45
Double-precision
floating point
  • Characters
    • A char is a single character that includes a letter, a digit, a punctuation mark, a tab, a space, or
      something like that. A char literal is a single character enclosed in single quotes such as char ch = ‘a’ ; char ch= ‘ 5 ’ ; char ch= ‘ * ’ ; etc.
  • Booleans
    • In Java, Booleans are logical variables, which can contain the value either true or false. Any other value cannot be assigned to a Boolean variable.

Java Keywords/Reserve Words

  • A keyword or reserved word in Java has a special & specific meaning and cannot be used as a user-defined identifier/variables/function name in any Java program, because they are used by the compiler for a specific purpose.
  • Each keyword has a specific and well-defined purpose.
  • When we use keywords as variable names in our Java program the compiler will generate errors.
  • All the Java keywords are represented in lowercase.
  • A list of Java keywords are – 
Slno. Keywords Description
01 abstract It declares that a class or method is abstract
02 assert Used to specify assertions 
03 boolean Declares that variable is a Boolean type
04 break Used to exit a loop before the end of the loop is reached
05 byte Declares that variable is byte type
06 case To represent different conditions in a switch statement
07 catch Used for handling exceptions, i.e., capture the exceptions thrown by some actions
08 char Declares that variable is a character type
09 class Signals the beginning of a class definition 
10 const This keyword is reserved by Java but now it is not in use
11 continue Prematurely return to the beginning of a loop
12 default The default action in a switch statement
13 do Begins a do-while loop
14 double Declares that variable is double type
15 else Signals the code to be executed when if condition executes to false 
16 extends Specifies the class base from which the correct class is inherited
17 final Declares that a class may not be extended or that a field or method may not be overridden
18 finally Declares a block of code guaranteed to be executed
19 float Declares a floating point variable
20 for Start a for loop
21 goto This keyword is reserved by Java but now it is not in use
22 if Keyword to represent a conditional statement
23 implements Declares that this class implements the given interface 
24 import permits access to a class or group of classes in a package
25 Instance of tests whether an object is an instance of a class
26 int Declares an integer variable
27 interface signals the beginning of an interface definition
28 long Declares a long integer variable
29 native Declares that a method that is implemented in native code
30 new Allocates memory to an object dynamically
31 package Defines the package to which this source code file belongs
32 private Declares a method or member variable to be private
33 protected Declares a class, method, or member variable to be protected
34 public Declares a class, method, or member variable to be public
35 return Returns a value from a method
36 short Declares a short integer variable
37 static Declares that a field or a method belongs to a class rather than an object
38 strictfp To declare that a method or class must be run with exact IEEE 754 semantics
39 super A reference to the parent of the current object
40 switch Tests for the truth of various possible cases
41 synchronized Indicates that a section of code is not thread-safe
42 this A reference to the current object
43 throws Declares the exceptions thrown by a method
44 transient Data should not be serialized
45 try Attempt an operation that may throw an exception
46 void  Declare that a method does not return a value 
47 volatile Warns the compiler that a variable changes asynchronously
48 while Begins a while loop
  • The words true, false, and null are used as reserved words but not in the list.
  • Keywords ‘const’ and ‘goto’ are currently not in use in the program.
  • In the list of Keywords, certain fresh Keywords have also been added, for example, Java 1.2 adds the strictfp keyword, Java 1.4 adds the assert keyword, etc.

Identifiers

  • Identifiers are nothing but the names of variables, methods, classes, packages, and interfaces.
  • In Java application programs, String, args, main, and System.out.println all are identifiers.
  • Identifiers must be composed of letters, numbers, the underscore ‘_’, and the dollar sign $. But identifiers should begin with a letter, the underscore, or a dollar sign.

Variables

  • It is a basic unit of storage in a program.
  • Variables represent memory locations in which values can be stored which are used in the program.
Types of Variables in Java

Java shows three kinds of variables –
Instance variables

    • Instance variables are used to define attributes or the state of a particular object and are used to store information needed by multiple methods in the objects.

Class/Global/Universal Variables

    • Class Variables are similar to instance variables, except their values apply to all the instances of a class (and to the class itself) rather than having different values for each object.
    • Class variables are global to a class and to all the instances of the class. They are useful for communicating between different objects of the same class or keeping track of global states.

Local/Private Variables –

    • Local variables are declared and used inside blocks as counters or in methods as temporary variables to store information needed by a single method.

Variable Declaration :

  • Before using any variable in the program, we must first declare it. Normally after the declaration, we can initialize (assign certain required values) them, if necessary.
  • We can also declare and assign a value to a variable at the same time.
  • Variable declarations can be put anywhere in the program code but just above the first use of the variable in the program code. However, it is common practice to place the variable declarations at the top of each code block.
  • Variable names in Java can only start with a letter, an underscore ( _), or a dollar sign ($). They cannot start with a number. Thus, a variable name can include letters or numbers or a combination of both but must start with a letter.
  • It is useful to name the program variables intelligently or according to their role in the program.
  • A variable name cannot contain any white space. To represent a variable with a gap, we can use the underscore symbol in between.
  • There is no limit to the length of a Java variable name.
  • A variable declaration specifies the datatype, the variable name, and, optionally, the default value for the variable.
  • The general syntax of the variable declaration is –
           Syntax = datatype variable-name ;
           Examples = byte x; short age; boolean attd; int x; 
  • We can also declare multiple variables of similar datatype of one type in one expression such as –
        Syntax = datatype variablename1, variablename2, variablename3 ;
        Examples =int x, rollno, age, salary;

Variable Initialization :

  • Once we have declared the data type of a variable, we can initialize it with some related data type’s value.
  • Variable Initialization may be of two types – (i) Static Variable Initialization and (ii) Dynamic Variable Initialization.

(i) Static Variable Initialization :

      • When we assign a certain value to the declared variable before run time, i.e. normally at the time of declaration or just after declaration in a single/double statement is called static initialization.
      • Syntax :-  variable name = some value;
        Example :- int rollno;
        rollno = 2801; int x=10;

(ii) Dynamic Variable Initialization :

      • When we assign a certain manipulated value to the declared variable during run time in a single statement is called dynamic initialization.
      • Syntax :-  variable name = some value;
class Example
{
    public static void main (String args [ ] )
      {
           int x= 50;       // here x is dynamically initialized variable.
           int y = 60;      // here y is a dynamically initialized variable.
           int z = x+y;     // here z is a dynamically initialized variable.
           System.out.println(“ The addition result is = ” + z);
       }
 }

Literals

  • Literals are nothing but pieces of Java code that indicate/contain explicit values.
  • For example “Hello IGNOU!” is a String literal. The double quote marks indicate to the compiler that this is a string literal. The quotes indicate the start and the end of the string, Here, the quote marks themselves are not a part of the string.
  • Similarly, Character Literals are enclosed in single quotes and they must have exactly one character.
  • TRUE and FALSE are boolean literals that mean true and false.
  • Number, double, long, and float literals also exist there.
Slno. Types of Literals Examples
01. Number Literals -45, 4L, 0777, 0XFF, 2.56F, 10e45, .36E-2
02. Boolean Literals TRUE, FALSE
03. Character Literals ‘a’, ‘#’, ‘3’, \n, \\, \”
04. String Literals “A string with a \t tab in it”
05. Double Literals 1.5, 45.6, 76.4E8
06. Long Literals 34L
07. Float Literals 45.6f, 76.4E8F, 1.5F

Constants

  • Constants are fixed values that are assigned directly to compatible variables.
  • The value of constants never changes during the program execution.
  • To declare constants in Java, the final keyword is used.
  • For example – The following statement defines an integer constant x containing a value of 100.
    final int x =100;
  • float area=3.141*r*r;

Loading

Categories: Java

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.