Posts

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

Arrays in C Language

Image
Suppose you want to store the 10 numbers. The one choice is to create a 10 different variable but it is difficult when number of variable are increases. So the next solution that is more feasible is array. An array is the collection of various similar type elements stored in contiguous memory locations. All the array elements share the common name. To uniquely identify each element an index or subscript is associated with the element. so the array is also known as subscript variable . In the given figure arrangement of array elements are shown. The address of the first element is 100. If the given array is of type integer and integer occupies 2 bytes then the next element in the array has address 102,104 etc as given in the figure. How to access array elements using subscript operator it is also shown in the given figure. Array index always start with 0. To access array element subscript operator is used.[] is called subscript operator.a[0] read a of 0 or 0th element of a C

Recursion

Image
A function is generally call another function to accomplish the task but sometimes a function can call itself to complete the task. This type of function is known as a recursive function and this feature is known as recursion. Let's take a short example of the recursive function. main(){ printf("Hello"); main(); } In the above program snippet main() function calls itself so main() is known as recursive function but the output of the above program snippet is "hello" prints indefinitely. The following program snippet improve the above logic and try to make it finite. main() { int i=1; if(i<=10) { printf("Hello"); i++; main(); } } If you thought that above logic print "Hello" 10 times then you are wrong because when program start i becomes one and of course the condition gets true and "Hello" gets printed then i becomes 2 by increment and then the main() function is again calle

Pointers in C Language

Image
Pointer is called the soul of c. The importance of pointer can be understood by the fact that most of the device driver or embedded system program are created in c. Pointer makes the c most usable and flexible programming. Due to the availability of the pointer, it is possible to write a program such as a device driver. These programs are also known as the device driver. To understand the pointer we have to first understand the memory organization of any variable. The following figure shows the fact. Whenever a variable stored in memory it occupies some memory space and every memory space has a unique number associated with it known as an address. Every byte in member has a different address. If we create an integer variable and suppose it occupy 2 bytes in memory, then two addresses are associated with it but its first address is enough to access it. so the address of next bytes is less useful. To access the address of a variable & (Address of) operator is used. To understa

Functions in C Language

Function is a group of statements which perfoms an specific task. We have used many functions such as printf(),scanf() and many more while developing a program. The purpose of a function can be understood by the example of printf(). Whenever you want to print anything on the output screen you use the printf() function. Actually to send the output on the screen is a difficult task and a program of multiple statements is need to write but fortunately, this is already done by Dennis Ritchie . So you can print anything by using a single line. Types of function In c various function are already created such as printf(),scanf(),sqrt(),pow() etc.These are known as predefined function or library function . C also provide the facility to create your own function. The functions created by the programmer are known as user defined function . Every function is followed by a pair of parenthesis(). While working with a function, some input needs to be supplied to the function. The input is g

Looping in C Programming

Introduction of loop Looping is a way to repeat a block of statement for the finite number of times. Here finite means that a loop should be terminated automatically or whenever the user wants. We also learn that a program is a collection of instruction. In C there are various control instructions that control the way to execute the program. Sequence control instruction: In which the program statements are executes in the order in which they are written. Decision making control instruction: In which we make a choice by using the if-else construct. Switch/case control instruction: Alternate choice of if-else construct. Loop control instruction: Used to perform the task repeatedly. We have already learned first 3 control instruction, and now we are going to discuss the last one loop control instruction. Let's start the discussion with an example. Suppose you want to print "Hello", then you write print("Hello"); once. But if I tell you, print &q

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