Programming with JAVA
What is Programming?
- A program is a collection of instructions that performs a specific task when executed by a computer. and the act of creating a program is known as programming.
- Programs are created to solve the problems, to automate the processes and to reduce repetitive and/or manual work.
To write these programs, you need a programming language.
There are many programming languages, each one provides various features. Based on the requirements of your programs and features of the languages, you can choose appropriate language for writing your programs.
In this blog, I will use java as Programming Language.
Q. What is Java?
- Java is one of the most popular High level programming languages.
- Java was created at Sun Microsystems, Inc. by James Gosling and his teams in 1991.
- It is being used by more than 10 million developers worldwide.
- More than 15 billion devices are powered by Java technology
- More than 125 million Java-based TV devices have been deployed.
- 97% of enterprise desktops run Java.
Java is High Level Programming language, but computers cannot understand high-level languages, they understand only binary language, i.e., 0’s and 1's.
So, whatever we will write in program, it will be converted into Binary form for computer to understand, this conversion is done by system software called compilers and interpreters.
High level code → binary form
- In Java, the program (source code) written by the programmer gets compiled and converted into byte code (compiled code) by the Java compiler.
- All the byte codes required for the program are then given to the interpreter. The interpreter reads the byte code line by line, converts it to binary form, also called as machine language or binary language, and then executes it.
- JDK (Java Development Kit) helps in this for execution of a Java program. It provides library support and Java Runtime Environment (JRE), the environment for developing and executing Java based applications and programs.
First java program:
- Every statement in Java program must end with semicolon (;).
- Every statement in Java must be present inside a method. A method is a block of code that performs a particular task.
- In the code given above, the method is named as main. Every program in Java must have a main method as the code execution starts from the main method.
- Every method in Java must be present inside a class. In the code given above, the class is named as Hello.
- Java is a case-sensitive programming language. E.g. — The word class should be written in lower case.
Execution of Java programs:
The development, compilation and execution of Java programs is taken care by JDK which contains 2 main components: Development tools and JRE.
The development tools consist of Java compiler and Java launcher.
- Java compiler (javac.exe) → It is the primary Java compiler. The compiler accepts Java source code and produces Java bytecode conforming to the Java Virtual Machine Specification (JVMS).
- Java launcher (java.exe) → It helps in launching a Java application during execution.
The Java Runtime Environment (JRE) contains Java Virtual Machine (JVM) and the Java standard library (Java Class Library).
- Java Virtual Machine (JVM) →It is the virtual machine that enables the computer to run Java programs.
- Java standard library (Java Class Library) → It is a set of dynamically loadable libraries that Java applications can call at run time. Java Platform is not dependent on a specific operating system and hence applications cannot rely on any of the platform-native libraries. So, the Java Platform provides a set of standard class libraries containing functions common to modern operating systems.
You can see in above picture step by step to run a java application.
* Java is platform Independent.
Q. What is platform Independent?
→ If a program written on a particular platform can run on other platforms without any recompilation, it is known as a platform independent program.
Features of Java:
Basics of java Programming:
Keyword:
- Every programming language has a set of keywords which are reserved words having a predefined meaning.
- Each keyword represents a specific functionality in the language.
- Some of keywords in java programming:
Variables:
- Data is stored in variables.
- A variable is a named memory location which holds some value.
- In Java, it is necessary to declare a variable along with a data type before it can be used.
- A variable inside main method needs to be initialized before use, there will be a compilation error if it is not initialized
How to declare a variable in Java:
e.g. :
***** In Java, variable names should be nouns starting with lowercase letter. If it contains multiple words, then every inner word must start with capital letter. This type of casing is called camel casing.
Some examples of variables are given below:
- mobileNumber
- name
- unitPrice
- paymentMode
- age
Identifiers:
- In Java, an identifier is the name given to a variable, method or class to uniquely identify it.
In Java, following sets of rules apply to the identifier name:
- It can contain alphanumeric characters([a-z], [A-Z], [0–9]), dollar sign ($), underscore (_)
- It should not start with a digit ([0–9]).
- It should not have spaces.
- It should not be a Java keyword.
- It is case-sensitive.
- It has no length restrictions.
e.g.:
Invalid identifiers: 1digit, my variable, if, else etc.
Valid Identifiers: digit, my_variable, $name, _age etc.
Data Types:
Q. What is Data Type?
→ Data type defines the type of data that can be stored in the variable and the memory required by it.
- There are two types of Data type:
Primitive Data type:
- Primitive data types are the basic data types defined in Java.
- There are 8 primitive data types as shown in the below table.
Points to be remember about primitive data types:
- Numeric and Boolean (true, false) values are written without quotes. E.g. int score = 85; boolean isQualified = true;
- The character value must be written in single quotes while assigning it to a character variable. E.g. char gender = ‘M’;
- A long value is assigned to the variable, suffixed with L (uppercase letter or lower-case letter L can be used). E.g., long salary = 500000L.
- A float value must be suffixed with F or f while assigning to the variable. E.g. float average = 78.6f;
Comments in java:
- Comments are those statements which are not executed by the compiler. Comments can be used to provide information about a variables or statement.
- There are two types of comments in Java:
- Single line comment (//): It is used to comment only one line.
- Multi-line comment (/**/): It is used to comment multiple lines of code.
Example for Data type and comment:
Non-primitive data type:
- Non-primitive data types include classes, arrays, interfaces etc. These can be predefined in Java library or user-defined in the programs. I will discuss this later in this blog.
- E.g. — String is a predefined class in Java library which is used to store a sequence of characters:
String customerName = “Jamie”;
Operators:
Q. what is Operator?
→ Operators are the symbols used to perform specific operations.
The operators are categorized as:
- Unary
- Arithmetic
- Relational
- Logical
- Ternary
- Assignment
- Bitwise
let’s see Operators one by one:
Unary Operator:
- Unary operators act upon only one operand and perform operations
e.g.
Arithmetic Operator:
Arithmetic operators are used to perform basic mathematical operations like addition, subtraction, multiplication and division.
e.g.
Relational Operator:
Relational operators are used to compare two values. The result of all the relational operations is either true or false.
Logical Operator:
Logical operators are used to combine two or more relational expressions or to negate the result of a relational expression.
e.g.
Ternary Operator (? :):
Ternary operator is used as a single line replacement for if-then-else (I will discuss later in this blog) statements and acts upon three operands.
Syntax:
<condition> ? <value if condition is true> : < value if condition is false>
e.g.
Assignment Operators:
Assignment operator is used to assign the value on the right-hand side to the variable on the left-hand side of the operator.
Bitwise Operator:(Most Important for DSA)
- Bitwise operators are used to perform manipulation of individual bits of a number.
You will now have a look at some of the bitwise operators and how they work.
- Bitwise OR (|):
It returns bit by bit OR of the input values. If either of the bits is 1, then it gives 1, else it gives 0.
E.g. — The output of 10 | 5 is 15.
- Bitwise AND (&)
It returns bit by bit AND of the input values. If both the bits are 1, then it gives 1, else it gives 0.
E.g. — The output of 10 & 5 is 0.
- Left shift operator (<<)
It takes two operators and left shifts the bits of the first operand. The second operand decides the number of places to shift. It fills 0 on voids left as a result.
- E.g. — The output of 10<<1 is 20 if the numbers are stored in 32 bit system.
10 is represented as 00000000 00000000 00000000 00001010.
After left shifting by 1 bit, the result becomes 00000000 00000000 00000000 000010100 which is 20.
The 0 that is highlighted is present because of the void.
Similarly, the output of 10<<2 is 40.
- Signed Right shift operator (>>)
It takes two operators and right shifts the bits of the first operand. The second operand decides the number of places to shift. It fills 0 on voids left as a result if the first operand is positive else it fills 1.
E.g. -
Example of positive number
The output of 10>>1 is 5.
10 is represented as 00000000 00000000 00000000 00001010.
After right shifting by 1 bit, the result becomes 00000000 00000000 00000000 00000101 which is 5.
Example of negative number
The output of -10>>1 is -5.
-10 is represented as 11111111 11111111 11111111 11110110.
After right shifting by 1 bit, the result becomes 11111111 11111111 11111111 11111011 which is -5.
- Unsigned Right shift operator (>>>):
It takes two operators and right shifts the bits of the first operand. The second operand decides the number of places to shift. It fills 0 on voids left as a result.
E.g. -
Example of positive number
The output of 10>>>1 is 5.
10 is represented as 00000000 00000000 00000000 00001010.
After right shifting by 1 bit, the result becomes 00000000 00000000 00000000 00000101 which is 5.
Example of negative number
The output of -10>>>1 is 214783643.
-10 is represented as 11111111 11111111 11111111 11110110.
After right shifting by 1 bit, the result becomes 01111111 11111111 11111111 11111011 which is 214783643.
Operator Precedence:
- Operators with higher precedence are evaluated before the operators with lower precedence.
- When an expression has two operators with the same precedence one after the other, the expression is evaluated according to their associativity. The associativity of operators can be left-to-right, right-to-left or no associativity.
e.g.
Thank you….
Type Conversion:
compatibility between data types:
- Whenever any operation is performed containing different data types, the data type of result is the largest data type in the expression.
For example:
- A variable will not be able to store a value if the value is more than the capacity of the datatype of the variable.
Example:
- Whenever, we try to store a bigger data type value into smaller data type, it will throw an error.
- When you assign the value of one data type to another or when you perform operation on two operands, their data types must be compatible with each other.
- If the data types are not compatible, then the data type of an operand needs to be converted.
This conversion is of two types:
- Implicit conversion
- Explicit conversion
Implicit Type Conversion:
- It is also known as Widening conversion.
- It is automatically performed by the compiler without the programmer’s interference.
- When a value of a data type with smaller range is assigned to a variable of a compatible data type with larger range then this conversion will apply.
Example:
- In Java, implicit type conversion happens as depicted in the diagram below. For example, if a short variable is assigned to int, long, float or double variable, it will get converted implicitly.
Explicit Conversion:
- It is used when you want to assign a value of larger range data type to a smaller range data type.
- It is also called Narrowing conversion; Programmer need to be cautious because there can be loss of data in some cases
Example:
So, we need to perform Explicit conversion:
Control Structures:
Q. What is Control Structures?
→ The statements which change the flow or order of execution of the instructions are called control structures.
There are three types of control structures:
- Sequential
- Selection
- Iteration
You can understand these three by the above picture.
continue…..