Full Transcript

JAVA PROGRAMMING LANGUAGE Java is a programming language and a platform. Java is a high level, robust, object-oriented and secure programming language. Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in the year 1995. James Gosling is...

JAVA PROGRAMMING LANGUAGE Java is a programming language and a platform. Java is a high level, robust, object-oriented and secure programming language. Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in the year 1995. James Gosling is known as the father of Java. Before Java, its name was Oak. Since Oak was already a registered company, so James Gosling and his team changed the name from Oak to Java. Platform: Any hardware or software environment in which a program runs, is known as a platform. Since Java has a runtime environment (JRE) and API, it is called a platform. Application According to Sun, 3 billion devices run Java. There are many devices where Java is currently used. Some of them are as follows: 1. Desktop Applications such as acrobat reader, media player, antivirus, etc. 2. Web Applications such as irctc.co.in, javatpoint.com, etc. 3. Enterprise Applications such as banking applications. 4. Mobile 5. Embedded System 6. Smart Card 7. Robotics 8. Games, etc. Types of Java Applications There are mainly 4 types of applications that can be created using Java programming: 1) Standalone Application Standalone applications are also known as desktop applications or window-based applications. These are traditional software that we need to install on every machine. Examples of standalone application are Media player, antivirus, etc. AWT and Swing are used in Java for creating standalone applications. 2) Web Application An application that runs on the server side and creates a dynamic page is called a web application. Currently, Servlet, JSP, Struts, Spring, Hibernate, JSF, etc. technologies are used for creating web applications in Java. 3) Enterprise Application An application that is distributed in nature, such as banking applications, etc. is called an enterprise application. It has advantages like high-level security, load balancing, and clustering. In Java, EJB is used for creating enterprise applications. 4) Mobile Application An application which is created for mobile devices is called a mobile application. Currently, Android and Java ME are used for creating mobile applications. Java Platforms / Editions There are 4 platforms or editions of Java: 1) Java SE (Java Standard Edition) It is a Java programming platform. It includes Java programming APIs such as java.lang, java.io, java.net, java.util, java.sql, java.math etc. It includes core topics like OOPs, String, Regex, Exception, Inner classes, Multithreading, I/O Stream, Networking, AWT, Swing, Reflection, Collection, etc. 2) Java EE (Java Enterprise Edition) It is an enterprise platform that is mainly used to develop web and enterprise applications. It is built on top of the Java SE platform. It includes topics like Servlet, JSP, Web Services, EJB, JPA, etc. 3) Java ME (Java Micro Edition) It is a micro platform that is dedicated to mobile applications. 4) JavaFX It is used to develop rich internet applications. It uses a lightweight user interface API. History of Java 1. History of Java 2. Java Version History The history of Java is very interesting. Java was originally designed for interactive television, but it was too advanced technology for the digital cable television industry at the time. The history of Java starts with the Green Team. Java team members (also known as Green Team), initiated this project to develop a language for digital devices such as set-top boxes, televisions, etc. However, it was best suited for internet programming. Later, Java technology was incorporated by Netscape. The principles for creating Java programming were "Simple, Robust, Portable, Platform-independent, Secured, High Performance, Multithreaded, Architecture Neutral, Object-Oriented, Interpreted, and Dynamic". Java was developed by James Gosling, who is known as the father of Java, in 1995. James Gosling and his team members started the project in the early '90s. Currently, Java is used in internet programming, mobile devices, games, e-business solutions, etc. Following are given significant points that describe the history of Java. 1) James Gosling, Mike Sheridan, and Patrick Naughton initiated the Java language project in June 1991. The small team of sun engineers called Green Team. 2) Initially it was designed for small, embedded systems in electronic appliances like set-top boxes. 3) Firstly, it was called "Greentalk" by James Gosling, and the file extension was.gt. 4) After that, it was called Oak and was developed as a part of the Green project. Why Java was named as "Oak"? 5) Why Oak? Oak is a symbol of strength and chosen as a national tree of many countries like the U.S.A., France, Germany, Romania, etc. 6) In 1995, Oak was renamed as "Java" because it was already a trademark by Oak Technologies. Why Java Programming named "Java"? 7) Why had they chose the name Java for Java language? The team gathered to choose a new name. The suggested words were "dynamic", "revolutionary", "Silk", "jolt", "DNA", etc. They wanted something that reflected the essence of the technology: revolutionary, dynamic, lively, cool, unique, and easy to spell, and fun to say. According to James Gosling, "Java was one of the top choices along with Silk". Since Java was so unique, most of the team members preferred Java than other names. 8) Java is an island in Indonesia where the first coffee was produced (called Java coffee). It is a kind of espresso bean. Java name was chosen by James Gosling while having a cup of coffee nearby his office. 9) Notice that Java is just a name, not an acronym. 10) Initially developed by James Gosling at Sun Microsystems (which is now a subsidiary of Oracle Corporation) and released in 1995. 11) In 1995, Time magazine called Java one of the Ten Best Products of 1995. 12) JDK 1.0 was released on January 23, 1996. After the first release of Java, there have been many additional features added to the language. Now Java is being used in Windows applications, Web applications, enterprise applications, mobile applications, cards, etc. Each new version adds new features in Java. Java Version History Many java versions have been released till now. The current stable release of Java is Java SE 10. 1. JDK Alpha and Beta (1995) 2. JDK 1.0 (23rd Jan 1996) 3. JDK 1.1 (19th Feb 1997) 4. J2SE 1.2 (8th Dec 1998) 5. J2SE 1.3 (8th May 2000) 6. J2SE 1.4 (6th Feb 2002) 7. J2SE 5.0 (30th Sep 2004) 8. Java SE 6 (11th Dec 2006) 9. Java SE 7 (28th July 2011) 10. Java SE 8 (18th Mar 2014) 11. Java SE 9 (21st Sep 2017) 12. Java SE 10 (20th Mar 2018) 13. Java SE 11 (September 2018) 14. Java SE 12 (March 2019) 15. Java SE 13 (September 2019) 16. Java SE 14 (Mar 2020) 17. Java SE 15 (September 2020) 18. Java SE 16 (Mar 2021) 19. Java SE 17 (September 2021) 20. Java SE 18 (to be released by March 2022) Since Java SE 8 release, the Oracle corporation follows a pattern in which every even version is release in March month and an odd version released in September month. Difference between JDK, JRE, and JVM JVM JVM (Java Virtual Machine) is an abstract machine. It is called a virtual machine because it doesn't physically exist. It is a specification that provides a runtime environment in which Java bytecode can be executed. It can also run those programs which are written in other languages and compiled to Java bytecode. JVMs are available for many hardware and software platforms. JVM, JRE, and JDK are platform dependent because the configuration of each OS is different from each other. However, Java is platform independent. There are three notions of the JVM: specification, implementation, and instance. The JVM performs the following main tasks:  Loads code  Verifies code  Executes code  Provides runtime environment JRE JRE is an acronym for Java Runtime Environment. It is also written as Java RTE. The Java Runtime Environment is a set of software tools which are used for developing Java applications. It is used to provide the runtime environment. It is the implementation of JVM. It physically exists. It contains a set of libraries + other files that JVM uses at runtime. The implementation of JVM is also actively released by other companies besides Sun Micro Systems. JDK JDK is an acronym for Java Development Kit. The Java Development Kit (JDK) is a software development environment which is used to develop Java applications and applets. It physically exists. It contains JRE + development tools. JDK is an implementation of any one of the below given Java Platforms released by Oracle Corporation:  Standard Edition Java Platform  Enterprise Edition Java Platform  Micro Edition Java Platform The JDK contains a private Java Virtual Machine (JVM) and a few other resources such as an interpreter/loader (java), a compiler (javac), an archiver (jar), a documentation generator (Javadoc), etc. to complete the development of a Java Application. How to set path in Java The path is required to be set for using tools such as javac, java, etc. If you are saving the Java source file inside the JDK/bin directory, the path is not required to be set because all the tools will be available in the current directory. However, if you have your Java file outside the JDK/bin folder, it is necessary to set the path of JDK. There are two ways to set the path in Java: 1. Temporary 2. Permanent 1) How to set the Temporary Path of JDK in Windows To set the temporary path of JDK, you need to follow the following steps:  Open the command prompt  Copy the path of the JDK/bin directory  Write in command prompt: set path=copied_path For Example: set path=C:\Program Files\Java\jdk1.6.0_23\bin 2) How to set Permanent Path of JDK in Windows For setting the permanent path of JDK, you need to follow these steps:  Go to MyComputer properties -> advanced tab -> environment variables -> new tab of user variable -> write path in variable name -> write path of bin folder in variable value -> ok -> ok -> ok First Java Program | Hello World Example public class Simple{ public static void main(String args[]){ System.out.println("Hello Java"); } } Compilation Flow: When we compile Java program using javac tool, the Java compiler converts the source code into byte code. Parameters used in First Java Program Let's see what is the meaning of class, public, static, void, main, String[], System.out.println().  class keyword is used to declare a class in Java.  public keyword is an access modifier that represents visibility. It means it is visible to all.  static is a keyword. If we declare any method as static, it is known as the static method. The core advantage of the static method is that there is no need to create an object to invoke the static method. The main() method is executed by the JVM, so it doesn't require creating an object to invoke the main() method. So, it saves memory.  void is the return type of the method. It means it doesn't return any value.  main represents the starting point of the program.  String[] args or String args[] is used for command line argument. We will discuss it in coming section.  System.out.println() is used to print statement. Here, System is a class, out is an object of the PrintStream class, println() is a method of the PrintStream class. We will discuss the internal working of System.out.println() statement in the coming section. Java Variables  A variable is a container which holds the value while the Java program is executed. A variable is assigned with a data type.  Variable is a name of memory location. There are three types of variables in java: local, instance and static.  There are two types of data types in Java: primitive and non-primitive. Variable A variable is the name of a reserved area allocated in memory. In other words, it is a name of the memory location. It is a combination of "vary + able" which means its value can be changed. 1. Ex. int data=50;//Here data is variable Types of Variables There are three types of variables in Java:  local variable  instance variable  static variable 1) Local Variable A variable declared inside the body of the method is called local variable. You can use this variable only within that method and the other methods in the class aren't even aware that the variable exists. A local variable cannot be defined with "static" keyword. 2) Instance Variable A variable declared inside the class but outside the body of the method, is called an instance variable. It is not declared as static. It is called an instance variable because its value is instance-specific and is not shared among instances. 3) Static variable A variable that is declared as static is called a static variable. It cannot be local. You can create a single copy of the static variable and share it among all the instances of the class. Memory allocation for static variables happens only once when the class is loaded in the memory. Example to understand the types of variables in java public class A { static int m=100;//static variable void method() { int n=90;//local variable } public static void main(String args[]) { int data=50;//instance variable } }//end of class Java Variable Example: Add Two Numbers 1. public class Simple{ 2. public static void main(String[] args){ 3. int a=10; 4. int b=10; 5. int c=a+b; 6. System.out.println(c); 7. } 8. } Declaring (Creating) Variables To create a variable, you must specify the type and assign it a value: Syntax type variableName = value; Java Identifiers Identifiers All Java variables must be identified with unique names. These unique names are called identifiers. Identifiers can be short names (like x and y) or more descriptive names (age, sum, totalVolume). Note: It is recommended to use descriptive names in order to create understandable and maintainable code: The general rules for naming variables are:  Names can contain letters, digits, underscores, and dollar signs  Names must begin with a letter  Names should start with a lowercase letter and it cannot contain whitespace  Names can also begin with $ and _ (but we will not use it in this tutorial)  Names are case sensitive ("myVar" and "myvar" are different variables)  Reserved words (like Java keywords, such as int or boolean) cannot be used as names Java Data Types Data types are divided into two groups: 1. Primitive data types - includes byte, short, int, long, float, double, boolean and char 2. Non-primitive data types - such as String, Arrays and Classes Primitive Data Types A primitive data type specifies the size and type of variable values, and it has no additional methods. There are eight primitive data types in Java: Data Type Size Description byte 1 byte Stores whole numbers from -128 to 127 short 2 bytes Stores whole numbers from -32,768 to 32,767 int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647 Stores whole numbers from -9,223,372,036,854,775,808 to long 8 bytes 9,223,372,036,854,775,807 Stores fractional numbers. Sufficient for storing 6 to 7 decimal float 4 bytes digits Stores fractional numbers. Sufficient for storing 15 decimal double 8 bytes digits boolean 1 bit Stores true or false values char 2 bytes Stores a single character/letter or ASCII values Java Numbers Numbers Primitive number types are divided into two groups: Integer types stores whole numbers, positive or negative (such as 123 or -456), without decimals. Valid types are byte, short, int and long. Which type you should use, depends on the numeric value. Floating point types represents numbers with a fractional part, containing one or more decimals. There are two types: float and double. Even though there are many numeric types in Java, the most used for numbers are int (for whole numbers) and double (for floating point numbers). However, we will describe them all as you continue to read. Non-Primitive Data Types Non-primitive data types are called reference types because they refer to objects. The main difference between primitive and non-primitive data types are:  Primitive types are predefined (already defined) in Java. Non-primitive types are created by the programmer and is not defined by Java (except for String).  Non-primitive types can be used to call methods to perform certain operations, while primitive types cannot.  A primitive type has always a value, while non-primitive types can be null.  A primitive type starts with a lowercase letter, while non-primitive types starts with an uppercase letter. Examples of non-primitive types are Strings, Arrays, Classes, Interface, etc. Java 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 example uses a single-line comment before a line of code: Example // This is a comment System.out.println("Hello World"); This example uses a single-line comment at the end of a line of code: Example System.out.println("Hello World"); // This is a comment Java Multi-line Comments Multi-line comments start with. Any text between will be ignored by Java. This example uses a multi-line comment (a comment block) to explain the code: Example System.out.println("Hello World"); Java Operators Operators are used to perform operations on variables and values. In the example below, we use the + operator to add together two values: Example int x = 100 + 50; Although the + operator is often used to add together two values, like in the example above, it can also be used to add together a variable and a value, or a variable and another variable: Example int sum1 = 100 + 50; // 150 (100 + 50) int sum2 = sum1 + 250; // 400 (150 + 250) int sum3 = sum2 + sum2; // 800 (400 + 400) Java divides the operators into the following groups:  Arithmetic operators  Assignment operators  Comparison operators  Logical operators  Bitwise operators Arithmetic Operators Arithmetic operators are used to perform common mathematical operations. Operator Name Description Example Try it + Addition Adds together two values x+y - Subtraction Subtracts one value from another x-y * Multiplication Multiplies two values x*y / Division Divides one value by another x/y % Modulus Returns the division remainder x%y ++ Increment Increases the value of a variable by 1 ++x -- Decrement Decreases the value of a variable by 1 --x Java Assignment Operators Assignment operators are used to assign values to variables. In the example below, we use the assignment operator (=) to assign the value 10 to a variable called x: Example int x = 10; The addition assignment operator (+=) adds a value to a variable: Example int x = 10; x += 5; A list of all assignment operators: Operator Example Same As = x=5 x=5 += x += 3 x=x+3 -= x -= 3 x=x-3 *= x *= 3 x=x*3 /= x /= 3 x=x/3 %= x %= 3 x=x%3 &= x &= 3 x=x&3 |= x |= 3 x=x|3 ^= x ^= 3 x=x^3 >>= x >>= 3 x = x >> 3 = y

Use Quizgecko on...
Browser
Browser