Posts

Showing posts with the label Java

Literals in Java

Image
Literals in Java int a = 5; In the above expression, a is a variable but 5 is literal. Literal can be of many types, we will discuss all types of literal one by one. Integral Literal Any integer constant is treated as integral literal. it can be written in 3 formats. int a = 10; (decimal format) int a = 010; (octal format, equivalent to 8) int a = 0x10; (hexadecimal format, equivalent to 16) Note: Octal constants can contain digits from 0 to 9. Hexadecimal constants can contains digits from 0 to 9 and a to f, where a to f can be uppercase or lowercase. Example - 1: int a = 10;    ( ✓ ) int a = 0678;    (✕) // Compile time error, number too large int a = 0777;   ( ✓ ) int a = 0xace;   ( ✓ ) int a = 0xBeer;    (✕) // Compile time error, number too large Example - 2: int a; a = 10 + 010 + 0x10; System.out.println(a); // 34, because 010 = 8 and 0x10 = 16 so 10 + 8 + 16 Important Notes We(programmer) have option to write integer constant in dec

Data types in Java

Image
What is Data type In Java every variable have some type.  For example in the code snippet below x should be int and b should be boolean type variable. x = 10; b = true; Similarly in Java every expression also have some type. For example in the following code snippet if a + b + c would result int type result because a, b and c are int type variables. int a, b, c, d; a = 1; b = 1; c = 1; d  = a + b + c; Strictly type checking  In Java, each type is strictly defined means byte have range from -128 to 127, so value out of from this range is not acceptable by byte type. Each assignment is strictly checked for type compatibility. Due to above two reason java is called strongly type checked language.   Primitive Data Types 8 Primitive Data types in Java Signed / Unsigned Data types Examples int a = 10;  ( ✓ ) int a = -10;  ( ✓ ) float x = -5.8;  ( ✓ ) char ch = -'a';  (✕) boolean b = -true;  (✕) boolean b = true;  ( ✓ ) Except boolean and

Identifiers

Image
What is identifier. A Name in Java program is called identifier, which can be used for identification purpose. It can be a class name, interface name, method name, label name, variable name etc. Example In the above example words marked with red underline are identifiers. As you can see there are 5 identifiers Test (Class Name) main (Method Name) String (Pre-defined Class Name) args (Variable Name) println(Method Name) Rules / Convention for Identifier Naming While defining Java identifier name, we have to follow some rules as listed below. Allowed characters in Java identifier are listed below.  A to Z (Uppercase letters) a to z (Lowercase letters) 0 to 9 (Digits) _ (Underscore) $ (Dollar sign) Example:  contact_number ( ✓ ) contact#   (✕) Identifier can't starts with digit. Example abc123 ( ✓ ) 123abc (✕) Java is a case sensitive language all variable listed below are different, but it is not a good programming

Popular posts from this blog

String in golang

Inline V/S Block Level Element

Floating point Data types in Go lang

Escape Sequence | Formatted Printing | Interview Questions

Sum of two numbers

Operators in C Language| Part-4

Printing in C programming

Arrays in C Language

Program to check a year is leap year or not.

Data types in Java