Java Programming involves learning about Java basics like features of Java language, JVM, bytecode, coding standards, data types, variables, operators, control structures, arrays, strings, wrapper classes, and more. Some key aspects covered are the Java design goals, Java source file structure, lexical issues, basic programming constructs, one and multi-dimensional arrays, the String, StringBuffer and StringBuilder classes.
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0 ratings0% found this document useful (0 votes)
91 views86 pages
Module 1 - Java Programming
Java Programming involves learning about Java basics like features of Java language, JVM, bytecode, coding standards, data types, variables, operators, control structures, arrays, strings, wrapper classes, and more. Some key aspects covered are the Java design goals, Java source file structure, lexical issues, basic programming constructs, one and multi-dimensional arrays, the String, StringBuffer and StringBuilder classes.
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 86
CSE1007-JAVA Programming
Module:1 Java Basics
• Java design goals
– Features of Java Language – JVM – Byte code – Java source file structure – Basic programming – constructs: lexical issues - data types - variables – Java coding standards - operators - control and looping constructs - – Arrays – one dimensional and multi-dimensional – enhanced for loop – String, String Buffer & String Builder, – Wrapper classes Agenta • What is Java and Java Platform with its types • History of Java and its Versions • Java Features and its Components • C++ Verses Java • First Java Program • JVM, JRE, JDK • Java Identifiers, Reserved Words, Variables • Java Data types, Operators, Examples What is Java • Java is a popular programming language. • JAVA was developed by Sun Microsystems Inc in the year 1991, • It was developed by James Gosling and Patrick Naughton. • Java is a Open Source, High level, Robust, Object-oriented and Secure • 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. • Java works on different platforms (Windows, Linux) Applications • There are many devices where Java is currently used and are as follows: • Desktop Applications such as acrobat reader, media player, antivirus, etc. • Web Applications such as irctc.co.in, javatpoint.com, etc. • Enterprise Applications such as banking applications. • Mobile • Embedded System • Smart Card • Robotics • Games, etc. • There are 4 Platforms or Editions of Java: • Java SE (Java Standard Edition) : It includes Java programming APIs such as java.lang, java.io, java.net, java.util, java.sql, java.math etc. • Java EE (Java Enterprise Edition) : It is built on the top of the Java SE platform. It includes topics like Servlet, JSP, Web Services, EJB. • Java ME (Java Micro Edition) : used to develop mobile applications. • JavaFX : used to develop rich internet applications(Graphics and media) Features of Java • Simple • Object-Oriented • Platform independent • Secured • Robust • Architecture neutral • High Performance • Multithreaded • Distributed • Dynamic • JVM- (Java Virtual Machine) is an abstract machine. It is called a virtual machine because it doesn't physically exist. • 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. The Java Runtime Environment is a set of software tools which are used for developing Java applications • JDK • JDK is an acronym for Java Development Kit. • 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. Compiled and interpreted Structure of Java Program /* FileName : "HelloWorld.java". */ class HelloWorld { // Your program begins with a call to main(). public static void main(String args[]) { System.out.println("Hello, World"); } } • The basic understanding of the code: • Class definition : uses the keyword class to declare a new class. HelloWorld is an • identifier and is the name of the class. • main() : Starting Point, every application must contain. • public: JVM can execute the method from anywhere. • static: Main method is to be called without object. • void: The main method doesn’t return anything. • String[]: command line arguments • System.out.println() is used to print statement. • Here, System is a class, out is the object of PrintStream class, println() is the method of PrintStream class Java Identifiers • They are used for identification purposes. • It is can be a class name, method name, variable name, or label. • For example :
• 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 to define Identifiers • The only allowed characters for identifiers are all alphanumeric characters([A-Z],[a-z],[0-9]), $(dollar sign) and _ (underscore). • For example “Deepu@” is not a valid java identifier as it contain ‘@’ special character. • Identifiers should not start with digits([0-9]). • For example “123Deepu” is a not a valid java identifier • Java identifiers are case-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. Reserved Words 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 Reserve Words Variables in Java • It is a name given to a memory location. It is the basic unit of storage in a program. • The value stored in a variable can be changed during program execution. • A variable is only a name given to a memory location, all the operations done on the variable effects that memory location. • In Java, all the variables must be declared before use. Types of Java Variables • Static • Instance • Local Static Variable • is declared as static and can create a single copy of static variable and share among all the instances of the class. • Memory allocation for static variable happens only once when the class is loaded in the memory. • Instance : declared inside the class.
• Local : declared inside the body of the method
and the other methods in the class aren’t even aware that the variable exists. Java Data types Operators in Java • Arithmetic Operators • Assignment Operator • Relational Operators • Logical Operators • Ternary Operator • Shift Operators • Bitwise Operators Type Casting in Java • Type casting is when you assign a value of one primitive data type to another type and are of two types in Java. – Widening Casting (automatically) : converting a smaller to a larger type. – Narrowing Casting (manually) : converting a larger to a smaller size type. Control and Looping constructs • A programming language uses control statements to control the flow of execution of program based on certain conditions If Condition nested if If else Example - nested if Example - if-else-if Ladder • Java supports three jump statement: break, continue and return Break Continue Return Loops in Java • Looping in programming languages is a feature which facilitates the execution of a set of instructions/functions repeatedly while some condition evaluates to true Arrays • An array is a collection of similar type(Homogeneous) of elements which has contiguous memory location. • Java array is an object which contains elements of a similar data type. • The elements of an array are stored in a contiguous memory location. • It is a data structure where can store similar elements. • It is static. • It is index-based, the first element of the array is stored at the 0th index, 2nd element is stored on 1st index and so on. Advantages : – Code Optimization: It makes the code optimized, we can retrieve or sort the data efficiently. – Random access: We can get any data located at an index position. Disadvantages : – Size Limit: We can store only the fixed size of elements in the array. – It doesn’t grow its size at runtime. • Types of Array in java – Single Dimensional Array – Multidimensional Array Single Dimensional Array in Java • Multidimensional Array in Java Data is stored in row and column based index Examples-1 - Java Arrays Output Output output Two dimensional Output output Reading from Console • Using Scanner Class : is used to parse primitive types and strings using regular expressions. (nextInt(), nextFloat(), . . . ). Strings • Strings are widely used in Java programming, • Strings are a sequence of characters. • In Java, Strings are treated as objects. • The String class is immutable, so that once it is created a String object cannot be changed. – import java.lang.* • Creating a String :- String greeting = "Hello world!"; String Methods charAt()-Returns the character at the specified index compareTo()-Compares this String to another concat()-Concatenates the specified string to the end of this string. indexOf()-returns the specified char value index length()-returns the length of string replace()-replaces all occurrences of the specified char value substring() toLowerCase()-returns a string in lowercase toUpperCase()- returns a string in uppercase trim()- removes beginning and ending spaces of this string Output Difference Between StringBuffer and String Builder • Java provides three classes to represent a sequence of characters: String, StringBuffer, and StringBuilder. • The String class is an immutable class whereas StringBuffer and StringBuilder classes are mutable. • StringBuffer is synchronized i.e. thread safe. • StringBuilder is non-synchronized i.e. not thread safe. • StringBuffer is less efficient than String Builder. • String Builder is more efficient than StringBuffer. StringBuffer charAt()-Returns the character at the specified index compareTo()-Compares this String to another concat()-Concatenates the specified string to the end of this string. indexOf()-returns the specified char value index length()-returns the length of string replace()-replaces all occurrences of the specified char value substring() toLowerCase()-returns a string in lowercase toUpperCase()- returns a string in uppercase trim()- removes beginning and ending spaces of this string String Builder Output