Posts

Showing posts with the label Java programming

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

Reserved Words in Java

Image
What is Reserved Words. In every language some words are already reserved to represent some action or meaning. For example in English some reserve words are listed below. Apple Run Cat Dog Eat Similarly in Java there are some words which meaning is already reserved, such words are called reserved words / keywords. In Java, Only 53 words are reserved words. Difference between Keywords and Reserved Literals If reserve words represents some functionality, then called Keywords . If reserve words represents some value, the called Reserve Literals . Reserve Literals (3) true (boolean value) false (boolean value) null (Object reference value) Keywords (50) Used Keywords (48) Unused Keyword (2) Goto Const All 53 Reserved Words List Data types (8) byte short int long float double boolean char Flow Control (11) if else switch case default while for do break continue return Modifiers (11) public private pr

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

Arrays in C Language

Data Types in Go language

Printing in C programming

Variable Naming & Scope of Variable

Escape Sequence | Formatted Printing | Interview Questions

Floating point Data types in Go lang

Overview of Go lang

Literals in Java