Introduction to Java + Installing Java JDK and IntelliJ IDEA for Java #1
Introduction to Java + Installing Java JDK and IntelliJ IDEA for Java
- Java is one of the most popular programming languages because it is used in various tech fields like app development, web development, client-server applications, etc.
- Java is an object-oriented programming language developed by Sun Microsystems of the USA in 1991.
- It was originally called Oak by James Goslin. He was one of the inventors of Java.
- Java = Purely Object-Oriented.
How Java Works?
- The source code in Java is first compiled into the bytecode.
- Then the Java Virtual Machine(JVM) compiles the bytecode to the machine code.
Java Installation:
Step 1: Downloading JDK
- JDK stands for Java Development Kit. It contains Java Virtual Machine(JVM) and Java Runtime Environment(JRE).
- JDK – Java Development Kit = Collection of tools used for developing and running java programs.
- JRE – Java Runtime Environment = Helps in executing programs developed in JAVA.
- Click here, and you will be redirected to the official download page of JDK.
- Select your operating system and download the file(executable file in case of Windows).
Step 2: Installing JDK
- Once the executable file is downloaded successfully, right-click and open the file.
- The executable file will start executing.
- Keep clicking on the Next button to install the JDK in default settings.
Step 3: Downloading IntelliJ IDEA :
- We need an Integrated Development Environment(IDE) to write and debug our code easily.
- IntelliJ IDEA is the best-suited IDE for writing Java code.
- Click here, and you will be redirected to the official download page of IntelliJ IDEA.
- Download the Community Version of the IntelliJ IDEA as it is free to use.
Step 4: Installing IntelliJ IDEA :
- Once the download completes, open the .exe file, and the installation process will begin.
- Click on the "Next" button to install the IntelliJ IDEA in the default location.
- Do not forget to check "Add launchers dir to the PATH," as shown in the below image.
- After this, click on the "Next" button and then click on the "Install" button.
Basic Structure of a Java Program #2
Basic Structure of a Java Program: Understanding our First Java Hello World Program
Basic Structure of a Java Program
package com.company;
// Groups classes
public class Main
{
// Entrypoint into the application
public static void main(String arg[])
{
System.out.println(“Hello World”);
}
}
Working of the "Hello World" program shown above :
- package com.company :
- Packages are used to group the related classes.
- The "Package" keyword is used to create packages in Java.
- Here, com.company is the name of our package.
- public class Main :
- In Java, every program must contain a class.
- The filename and name of the class should be the same.
- Here, we've created a class named "Main".
- It is the entry point to the application.
- public static void main(String[]args){..} :
- This is the main() method of our Java program.
- Every Java program must contain the main() method.
- System.out.println("Hello World"):
- The above code is used to display the output on the screen.
- Anything passed inside the inverted commas is printed on the screen as plain text.
Naming Conventions
- For classes, we use Pascal Convention. The first and Subsequent characters from a word are capital letters (uppercase).
Example: Main, MyScanner, MyEmployee, Codervishal
- For functions and variables, we use camelCaseConvention. Here the first character is lowercase, and the subsequent characters are uppercase like myScanner, myMarks, Codervishal.
Variables and Data Types in Java #3
Java Tutorial: Variables and Data Types in Java Programming
Just like we have some rules that we follow to speak English (the grammar), we have some rules to follow while writing a Java program. This set of these rules is called syntax. It’s like Vocabulary and Grammar of Java.
Variables
- A variable is a container that stores a value.
- This value can be changed during the execution of the program.
- Example: int number = 8; (Here, int is a data type, the number is the variable name, and 8 is the value it contains/stores).
Rules for declaring a variable name
We can choose a name while declaring a Java variable if the following rules are followed:
- Must not begin with a digit. (E.g., 1arry is an invalid variable)
- Name is case sensitive. (Vishal and harry are different)
- Should not be a keyword (like Void).
- White space is not allowed. (int Coder vishal is invalid)
- Can contain alphabets, $character, _character, and digits if the other conditions are met.
Data Types
Data types in Java fall under the following categories
1. Primitive Data Types (Intrinsic)
2. Non-Primitive Data Types (Derived)
Primitive Data Types
Java is statically typed, i.e., variables must be declared before use. Java supports 8 primitive data types:
Data Type | Size | Value Range |
1. Byte | 1 byte | -128 to 127 |
2. short | 1 byte | -32,768 to 32,767 |
3. int | 2 byte | -2,147,483,648 to 2,147,483,647 |
4. float | 4 byte | 3.40282347 x 1038 to 1.40239846 x 10-45 |
5. long | 8 byte | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
6. double | 8 byte | 1.7976931348623157 x 10308, 4.9406564584124654 x 10-324 |
7. char | 2 byte | 0 to 65,535 |
8. boolean | Depends on JVM | True or False |
How to choose data types for our variables
In order to choose the data type, we first need to find the type of data we want to store. After that, we need to analyze the min & max value we might use.
Java Tutorial: Literals in Java #4
Literals
A constant value that can be assigned to the variable is called a literal.
- 101 – Integer literal
- 10.1f – float literal
- 10.1 – double literal (default type for decimals)
- ‘A’ – character literal
- true – Boolean literal
- “Vishal” – String literal
Keywords
Words that are reserved and used by the Java compiler. They cannot be used as an Identifier.
{You can visit docs.oracle.com for a comprehensive list}
package com.company;
public class literals {
public static void main(String[] args) {
byte age = 34;
int age2 = 56;
short age3 = 87;
long ageDino = 5666666666666L;
char ch = 'A';
float f1 = 5.6f;
double d1 = 4.66;
boolean a = true;
System.out.print(age);
String str = "Vishal";
System.out.println(str);
}
}
Java Tutorial: Getting User Input in Java #5
Reading data from the Keyboard :
Scanner class of java.util package is used to take input from the user's keyboard.The Scanner class has many methods for taking input from the user depending upon the type of input. To use any of the methods of the Scanner class, first, we need to create an object of the Scanner class as shown in the below example :
import java.util.Scanner; // Importing the Scanner class
Scanner sc = new Scanner(System.in); //Creating an object named "sc" of the Scanner class.
Taking an integer input from the keyboard :
Scanner S = new Scanner(System.in); //(Read from the keyboard)
int a = S.nextInt(); //(Method to read from the keyboard)
Program :-
package com.company;
import java.util.Scanner;
public class TakingInput {
public static void main(String[] args) {
System.out.println("Taking Input From the User");
Scanner sc = new Scanner(System.in);
// System.out.println("Enter number 1");
// int a = sc.nextInt();
// float a = sc.nextFloat();
// System.out.println("Enter number 2");
// int b = sc.nextInt();
// float b = sc.nextFloat();
// int sum = a +b;
// float sum = a +b;
// System.out.println("The sum of these numbers is");
// System.out.println(sum);
// boolean b1 = sc.hasNextInt();
// System.out.println(b1);
// String str = sc.next();
String str = sc.nextLine();
System.out.println(str);
}
}
Operators
An operator is a symbol that the compiler to perform a specific operation on operands.
Example : a + b = c
In the above example, 'a' and 'b' are operands on which the '+' operator is applied.
Types of operators :
Arithmetic Operators :
Arithmetic operators are used to perform mathematical operations such as addition, division, etc on expressions.
Arithmetic operators cannot work with Booleans.
% operator can work on floats and doubles.
Let x=7 and y=2
Operator Description Example
+ (Addition) Used to add two numbers x + y = 9
- (Subtraction) Used to subtract the right-hand side value from the left-hand side value x - y = 5
* (Multiplication) Used to multiply two values. x * y = 14
/ (Division) Used to divide left-hand Value by right-hand value. x / y = 3
% (Modulus) Used to print the remainder after dividing the left-hand side value from
the right-hand side value. x % y = 1
++ (Increment) Increases the value of operand by 1. x++ = 8
-- (Decrement) Decreases the value of operand by 1. y-- = 1
2. Comparision Operator
As the name suggests, these operators are used to compare two operands.
Let x=7 and y=2
Operator Description Example
== (Equal to) Checks if two operands are equal. Returns a boolean value. x == y --> False
!= (Not equal Checks if two operands are not equal. Returns a boolean value. x != y --> True
> (Greater than) Checks if the left-hand side value is greater than the right-hand side value. Returns a boolean value. x > y --> True
< (Less than) Checks if the left-hand side value is smaller than the right-hand side value. Returns a boolean value. x < y --> False
>=(Greater than or equal to) Checks if the left-hand side value is greater than or equal to the right-hand side value. Returns a boolean value. x >= y --> True
<= (Less than or equal to) Checks if the left-hand side value is less than or equal to the right-hand side value. Returns a boolean value. x <= y -->False
3. Logical Operators :
These operators determine the logic in an expression containing two or more values or variables.
Let x = 8 and y =2
&& (logical and) Returns true if both operands are true.
x<y && x!=y --> True
|| (logical or) Returns true if any of the operand is true. x<y && x==y --> True
! (logical not) Returns true if the result of the expression is false and vice-versa
!(x<y && x==y) --> False
4. Bitwise Operators :
These operators perform the operations on every bit of a number.
Let x =2 and y=3. So 2 in binary is 100, and 3 is 011.
Operator Description Example
& (bitwise and) 1&1 =1, 0&1=0,1&0=0,1&1=1, 0&0 =0 (A & B) = (100 & 011) = 000
| (bitwise or) 1&0 =1, 0&1=1,1&1=1, 0&0=0 (A | B) = (100 | 011 ) = 111
^ (bitwise XOR) 1&0 =1, 0&1=1,1&1=0, 0&0=0 (A ^ B) = (100 ^ 011 ) = 111
<< (left shift) This operator moves the value left by the number of bits specified. 13<<2 = 52(decimal)
>> (right shift) This operator moves the value left by the number of bits specified. 13>>2 = 3(decimal)
package com.company;
public class Operators {
public static void main(String[] args) {
// 1. Arithmetic Operators
int a = 4;
// int b = 6 % a; // Modulo Operator
// 4.8%1.1 --> Returns Decimal Remainder
// 2. Assignment Operators
int b = 9;
b *= 3;
System.out.println(b);
// 3. Comparison Operators
// System.out.println(64<6);
// 4. Logical Operators
// System.out.println(64>5 && 64>98);
System.out.println(64>5 || 64>98);
// 5. Bitwise Operators
System.out.println(2&3);
// 10
// 11
// ----
// 10
}
}
Java Tutorial: Associativity of Operators in Java
Comments
Post a Comment