Introduction to Java
JAVA was developed by James Gosling and Patrick Naughton at Sun Microsystems Inc in 1991, later in 2010 acquired by Oracle Corporation. It is a simple programming language. Writing, compiling, and debugging a program is easy in java. It helps to create modular programs and reusable code. That is the reason that more than 3 billion devices run Java, it is used for
- Mobile applications (especially Android apps)
- Desktop applications
- Web applications
- Web servers and application servers
- Games development
Importance of Java
-
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.)
-
It is one of the most popular programming language in the world
-
It is easy to learn and simple to use
-
It is open-source and free
-
It is secure, fast and powerful
-
It has a huge community support (tens of millions of developers)
-
Java is an object oriented language which gives a clear structure to programs and allows code to be reused, lowering development costs.
-
As Java is close to C++ and C#, it makes it easy for programmers to switch to Java or vice versa
Steps for Installation of JDK and Netbeans
- Downloading JDK and Netbeans
- Download JDK from
https://www.oracle.com/java/technologies/javase-jdk8-downloads.html
- Select the 64 bit or 32 bit architecture and also the operating system you are using. in my case the last one i selected
- After downloading, then do Installation of JDK.
- Simply leave every thing to its default and click next until you finish.
Netbeans download
- Download netbean Java editor from the link given
https://netbeans.org/downloads/8.2/rc/
- For downloading click on the Download button below the All
- After downloading do Installation of Java Editor Netbeans.
- Simply leave every thing to its default and click next until you finish.
- Note :- Tutorial video link is given
- Downloading JDK and Netbeans
Starting Programming
- In Java, every application begins with a class name, and that class must match the filename.
- Let’s create our first Java file, called Helo.java, which can be done in any text editor like eclipse, Netbeans, notepad etc.
- As Java is case sensitive so Helo and helo have different meaning.
- The file should contain a “Hello World” message, which is written with the following code given in structure of java program:
Explain Creating a project
- Creating new project isn’t very hard.
- Click New Projectin File menu or Welcome window.
- Choose Category General, and Project Java Application(Mostly the default one). Click Next
- The New Java Applicationdialog box
1. Project Name. This is not only the name of the project, but also used to create a folder by that name, and after mangling the name, it becomes the default package name. This field starts with an unhelpful name like “JavaApplication” – change it.2. Choose a Project Location. This is the folder in which your project folder will be created. You can have many projects in this folder.
3. Set as Main Project. Check this box. This indicates that the project will contain a main method and can be run as an application.
4. Create Main Class. Check this box. Clicking Finish creates a class from a standard template.
5. Package name and main class name are shown in the field after the Create Main Class check box. The package name is the part before the dot.
6. Finish. All project files will be created when you click this.
Note: he left panel now shows your project, and the right panel shows a source file.
Structure of Java Program
public class Helo
{
public static void main(String [] args)
{
System.out.print(“Hello World”);
}
}
public
Is an access specifier, mean these words show who can access this method, variable etc. who cannot access. We have three different type of Access specifier like Private, Public and protected.
static
Static mean the one whose values is not change.
void
void is actually showing the return type of method. If void is used then it means to return nothing, instead of that if return type is used then the method will return the data according the type specified. Like int, double etc.
main()
The main method is from where the execution actually starts and follows the order specified for the following statements. This is the name of java main method. It’s fixed and when we start a java program, it looks for the main method.
The main() method is required and you will see it in every Java program:
public static void main(String[] args)
Any code inside the main() method will be executed. You don’t have to understand the keywords before and after main. we will get to know them a little bit while reading this lecture. For now, just remember that every Java program has a class name which must match the filename, and that every program must contain the main() method.
String [] args
This is the parameter used inside main method. The parameter name is args and data type is String and array is used. In short inside the main method there is a parameter which is a string type array namely args.
System.out.println()
Inside the main() method, we can use the println() method to print a line of text to the screen:
Java Language Fundamental
Basic about Class Name
All code that runs in Java must be inside a class. In this example, class name is Helo. A class should always start with an uppercase first letter. When saving file the name of the java file must match the class name, save it using the class name and add “.java” to the end of the filename.
Comments:
Comments can be used to explain Java code, and to make it more readable. It can also be used to prevent execution when testing alternative code.
Single-line comments
Single-line comments start with two forward slashes (//). Any text between // and the end of the line is ignored by Java (will not be executed).
//This is a comment at the start of line
System.out.println(“Hello World”); //This is a comment at the end of line
Multi-line Comments
Multi-line comments start with /* and ends with */. Any text between /* and */ will be ignored by Java. This example uses a multi-line comment (a comment block) to explain the code:
/* sjdksdjLASJDDfds
akfakfjkdjf */
Keywords and identifiers
Reserved words (Key words)
Keywords are words that have already been defined for Java compiler. They have special meaning for the compiler. Java Keywords must be in your information because you cannot use them as a variable, class or a method name. You can’t use keyword as identifier in your Java programs, its reserved words in Java library and used to perform an internal operation. There are 53 reserved words in Java.
Abstract | Assert | Boolean | break | Null |
Byte | Case | Catch | char | false |
Class | Const | Continue | default | true |
Do | Double | Else | enum | While |
Extends | Final | Finally | float | Volatile |
For | Goto | If | implements | void |
Import | Instanceof | Int | interface | Try |
Long | Native | New | package | Transient |
Private | Protected | Public | return | Throws |
Short | Static | Strictfp | super | Throw |
Switch | synchronized | This |
true, false and null are not reserved words but cannot be used as identifiers, because it is literals of built-in types.
Identifiers
In programming languages, identifiers are used for identification purpose. In Java, an identifier can be a class name, method name, variable name or a label. For example :
public class Test
{
public static void main(String[] args)
{
int a = 20;
}
}
In the above java code, we have 5 identifiers namely:
- Test: class name.
- main: method name.
- String: predefined class name.
- args: variable name.
- a: variable name.
Rules for defining Java Identifiers
There are certain rules for defining a valid java identifier. These rules must be followed, 0therwise we get compile-time error. These rules are also valid for other languages like C,C++.
- The only allowed characters for identifiers are all alphanumeric characters([A-Z],[a-z],[0-9]), ‘$‘(dollar sign) and ‘_‘ (underscore).For example “geek@” is not a valid java identifier as it contain ‘@’ special character.
- Identifiers should notstart with digits([0-9]). For example “123geeks” is a not a valid java identifier.
- Java identifiers arecase-sensitive.
- There is no limit on the length of the identifier but it is advisable to use an optimum length of 4 – 15 letters only.
- ReservedWords can’t be used as an identifier. For example “int while = 20;” is an invalid statement as while is a reserved word. There are 53 reserved words in Java.
Examples of valid identifiers :
MyVariable
MYVARIABLE
myvariablexix1i1,
_myvariable
$myvariable
sum_of_array
geeks123
Examples of invalid identifiers :
My Variable // contains a space
123geeks // Begins with a digita
+c // plus sign is not an alphanumeric character
variable-2 // hyphen is not an alphanumeric character
sum_&_difference // ampersand is not an alphanumeric character
Data Types
Data types are the means for the tasks related to identifying and assessing the type of data. Java is rich in data types which allows the programmer to select the appropriate type needed to build variables of an application.
Data Type important
- Every variable in Java has a data type which tells the compiler what type of variable it as and what type of data it is going to store.
- Data type specifies the size and type of values.
- Information is stored in computer memory with different data types.
- Whenever a variable is declared, it becomes necessary to define a data type that what will be the type of data that variable can hold.
Data Types in Java
Data Types in java are
- Primary Data Type/Primitive Data Type
Java supports eight primitive data types: byte, short, int, long, float, double, char and boolean.
These eight data types are further classified into four groups:
-
-
- Integer,
- Relational Numbers(Floating point)
- Characters
- Boolean (Conditional)
-
- Non-Primitive Data Type
-
-
-
-
- Classes
- Interface
- Arrays
-
-
-
Integer Type
Integer is the whole number without any fractional point. It can hold whole numbers such as 196, -52, 4036, etc. Java supports four different types of integers, these are:
Type | Contains | Default | Size | Range |
Byte | Signed integer | 0 | 8 bit or 1 byte | -27 to 27-1 or -128 to 127 |
Short | Signed integer | 0 | 16 bit or 2 bytes | -215 to 215-1 or -32,768 to 32767 |
Int | Signed integer | 0 | 32 bit or 4 bytes | -231 to 231-1 or -2147,483,648 to 2147,483,647 |
Long | Signed integer | 0 | 64 bit or 8 bytes | -263 to 263-1 or -9223,372,036,854,755,808 to 9223,372,036,854,755,807 |
Rational Number
It is used to hold whole numbers containing fractional part such as 36.74, or -23.95 (which are known as floating point constants). There are two types of floating point storage in java. These are:
Type | Contains | Default | Size | Range |
Float | IEEE 754 floating point single-precision |
0.0f | 32 bit or 4 bytes |
±1.4E-45 to ±3.40282347E+38F |
Double | IEEE 754 floating point double-precision |
0.0 | 64 bit or 8 bytes |
±439E-324 to ±1.7976931348623157E+308 |
Characters
It is used to store character constants in memory. Java provides a character data type called char whose type consumes a size of two bytes but can hold only a single character.
Type | Contains | Default | Size | Range |
Char | Unicode character unsigned |
\u0000 | 16 bits or 2 bytes |
0 to 216-1 or \u0000 to \uFFFF |
Boolean
Boolean type is used to test a particular condition during program execution. Boolean variables can take either true or false and is denoted by the keyword boolean and usually consumes one byte of storage.
Type | Contains | Default | Size | Range |
Boolean | true or false | False | 1 bit | true or false |
Variables
Variables are the identifier of the memory location, which used to save data temporarily for later use in the program. During execution of a program, values can be stored in a variable, and the stored value can be changed. In Java programming, it is necessary to declare the variable before being used.
Declaration of Variable in Java
Declaring a variable means what kind of data it will store. Variables display named storage locations, whose values can be changed during the execution of the program. It is the basic unit of storage in a Java program.
Syntax:
type variable_name;
or
type variable_name, variable_name, variable_name;
Here’s the meaning of type is a data type. It specifies what type of data the variable will hold.
Example:
// variable definition
int width, height=5;
char letter=’C’;
float age, area;
double d;
Initialization of variable in java
This means assigning a value to variables. In Java, you can assign a value to variables in two ways:
- Static – This means that the memory is determined for variables when the program starts.
- Dynamic – Dynamic means that in Java, you can declare variables anywhere in the program, because when the statement is executed the memory is assigned to them.
Example:
// actual initialization
width = 10;
age = 26.5;
The variable name needs to be chosen by the programmer in a meaningful way so that it reflects what it is representing a program.
Rules for declaring variable in java
- A variable name can consist of Capital letters A-Z, lowercase letters a-z, digits 0-9, and two special characters such as underscore and dollar Sign.
- The first character must be a letter.
- Blank spaces cannot be used in variable names.
- Java keywords cannot be used as variable names.
- Variable names are case-sensitive.
Scope of Variables in Java
Variable Scope means – That limit, as far as the variable can be used.
In Java there are various types of variable scope:
- Local variables
- Instance variables
- Class/Static variables
Local variables
A variable that is declared within the method that is called local variables. It is defined in method or other statements, such as defined and used within the cache block, and outside the block or method, the variable cannot be used.
Instance variables
A non-static variable that is declared within the class but not in the method is called instance variable. Instance variables are related to a specific object; they can access class variables.
Class/Static variables
A variable that is declared with static keyword in a class but not in the method is called static or class variable.
Example:
class A
{
int amount = 100; //instance variable
static int pin = 2315; //static variable
public static void main(String[] args)
{
int age = 35; //local variable
}
}
Example:
public class Example
{
int salary; // Instance Variable
public void show()
{
int value = 2; // Local variable
value = value + 10;
System.out.println(“The Value is : ” + value);
salary = 10000;
System.out.println(“The salary is : ” + salary);
}
public static void main(String args[])
{
Example eg = new Example();
eg.show();
}
}
Example of Data Type & variable declaration
class DataTypes
{
public static void main(String args[])
{
byte byteVar = 5;
short shortVar = 20;
int intVar = 30;
long longVar = 60;
float floatVar = 20;
double doubleVar = 20.123;
boolean booleanVar = true;
char charVar =’W’;
System.out.println(“Value Of byte Variable is ” + byteVar);
System.out.println(“Value Of short Variable is ” + shortVar);
System.out.println(“Value Of int Variable is ” + intVar);
System.out.println(“Value Of long Variable is ” + longVar);
System.out.println(“Value Of float Variable is ” + floatVar);
System.out.println(“Value Of double Variable is ” + doubleVar);
System.out.println(“Value Of boolean Variable is ” + booleanVar);
System.out.println(“Value Of char Variable is ” + charVar);
}
}
Program Output:
Value Of byte Variable is 5
Value Of short Variable is 20
Value Of int Variable is 30
Value Of long Variable is 60
Value Of float Variable is 20.0
Value Of double Variable is 20.123
Value Of boolean Variable is true
Value Of char Variable is W
Lab # 2
- Run First Program
- Understanding structure of computer program
- Practice class creation
- Practice Object Creation
- declaring two integer data Type and Adding two number program
Lecture No
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Like!! I blog quite often and I genuinely thank you for your information. The article has truly peaked my interest.