unit 1.pdf
Document Details
Uploaded by PleasingSugilite7295
SRM Institute of Science and Technology
Tags
Related
- Topic 1.6 Running Program with Objects PDF
- CS0070 Object-Oriented Programming Module 1 PDF
- Object Oriented Programming (2nd Year 1st Sem) PDF
- Introduction to Java Programming and Data Structures (2019) by Y. Daniel Liang - PDF
- VILLASAN Object-Oriented Programming PDF
- Introduction To Object Oriented Programming PDF
Full Transcript
Origin of JAVA JAVA was developed by sun Microsystems Inc. in 1991, it was developed by “James Gosling” and “Patrick Naughton”. James Gosling , Mike Sheridan and Patrick Naughton started the Java language project in June 1991. Java was initially called as “Oak”. Later it was renamed as Java, from...
Origin of JAVA JAVA was developed by sun Microsystems Inc. in 1991, it was developed by “James Gosling” and “Patrick Naughton”. James Gosling , Mike Sheridan and Patrick Naughton started the Java language project in June 1991. Java was initially called as “Oak”. Later it was renamed as Java, from Java Coffee. Impact Of Java Over Internet ▪ Platform Independence: Java's ability to run on various platforms made internet applications accessible across different devices and operating systems. ▪ Applets: Java Applets brought interactivity to web browsers, enhancing early web experiences. ▪ Mobile Development: Java is a key language for Android app development, a major presence on the internet ▪ Web Services: Java supports web services, facilitating data and service exchange over the internet. ▪ Cloud Computing: Java is supported by cloud platforms like AWS and GCP, simplifying internet application development. Community and Evolution: Java's extensive developer community and ongoing updates ensure its internet relevance Java’s magic: ByteCode , JVM ▪ Bytecode: Java bytecode is the instruction set for the Java Virtual Machine. Once a java program is compiled, the compiler creates the java bytecode. Java bytecode is the machine code in the form of a “.class” file by the compiler. Java bytecode is used to achieve platform independence. ▪ JVM: Java Virtual Machine is referred as JVM. Writing of the program done by java programmer. Compilation of program is done by javac compiler, javac is the primary java compiler in Java Development Kit (JDK). It takes java program as input and generates bytecode as output. Introduction to Java Buzzword Java is the most popular object-oriented programming language. Java has many advanced features, a list of key features is known as Java Buzz Words. The java team has listed the following terms as java buzz words: Simple Secure Portable Object-oriented Robust Architecture-neutral (or) Platform Independent Multi-threaded Interpreted High performance Distributed Dynamic Java Buzzwords ▪ Simple: A very simple, easy to learn and understand language for programmers who are already familiar with OOP concepts. Java’s programming style and structure follows the lineage of C, C++ and other similar languages makes the use of java efficiently. ▪ Object-oriented: Java is object oriented. Java inherits features of C++. OOP features of java are influenced by C++. OOP concept forms the heart of java language that helps java program in survive the inevitable changes accompanying software development. ▪ Secure, Portable and Robust: Java programs are safe and secure to download from internet. At the core of the problem is the fact that malicious code can cause its damage due to unauthorized access gained to system resources. Java achieved this protection by confining a program to the Java execution environment and not allowing it access to other parts of the computer. The same code must work on all computers. Therefore, some means of generating portable executable code was needed. The multi-platform environment of the Web places extraordinary demands on a program, because the program must execute reliably in a variety of systems. Thus, the ability to create robust programs was given a high priority in the design of Java. To gain reliability, Java restricts a few key areas and forces to find your mistakes early in program development. At the same time, Java frees a programmer from having to worry about many of the most common causes of programming errors. Multithreaded: Java supports multithreaded programming, which allows a programmer to write programs that performs multiple tasks simultaneously. The Java run-time system comes with an elegant and sophisticated solution for multi-process synchronization that helps to construct smoothly running interactive systems. Architecture-neutral: Java was designed to support applications on networks composed of a variety of systems with a variety of CPU and operating system architectures. With Java, the same version of the application runs on all platforms. The Java compiler does this by generating bytecode instructions which have nothing to do with particular processor architecture. Rather, they are designed to be both easy to interpret on any machine and easily translated into native machine code on the fly. Interpreted & High performance: Java enables the creation of cross-platform programs by compiling into an intermediate representation called Java bytecode. This code can be executed on any system that implements the Java Virtual Machine. Most previous attempts at crossplatform solutions have done so at the expense of performance. The Java bytecode was carefully designed so that it would be easy to translate directly into native machine code for very high performance by using a just-in-time compiler. Java run-time systems that provide this feature lose none of the benefits of the platform-independent code. Distributed: Java is designed for the distributed environment of the Internet because it handles TCP/IP protocols. In fact, accessing a resource using a URL is not much different from accessing a file. Java also supports Remote Method Invocation (RMI). This feature enables a program to invoke methods across a network. Dynamic: Java programs carry with them substantial amounts of run-time type information that is used to verify and resolve accesses to objects at run time. This makes it possible to dynamically link code in a safe and expedient manner. This is crucial to the robustness of the Java environment, in which small fragments of byte code may be dynamically updated on a running system. Evolution of Java Introduction to Object Oriented Concepts of Java Java is a widely-used programming language known for its strong support for object- oriented programming (OOP) concepts. Object-oriented programming is a paradigm that revolves around the concept of objects, which are instances of classes. Here's an introduction to the core OOP o Class o Object o Abstraction o Encapsulation o Polymorphism o Compile time Polymorphism o Runtime Polymorphism o Inheritance Encapsulation, Polymorphism , Inheritance Encapsulation : Classes and packages are both means of encapsulating and containing the name space and scope of variables and methods. Packages act as containers for classes and other subordinate packages. Classes act as containers for data and code. The class is Java’s smallest unit of abstraction. Because of the interplay between classes and packages, Java addresses four categories of visibility for class members: Subclasses in the same package Non-subclasses in the same package Subclasses in different packages Classes that are neither in the same package nor subclasses The three access modifiers, private, public, and protected, provide a variety of ways to produce the many levels of access required by these categories. Polymorphism: Compile time Polymorphism: Method Overloading is a mechanism in which a class allows more than one method with same name but with different prototype. Multiple methods can be created by changing the no. of parameters, type of parameters, order of parameters or combination of any. The method binding is done by the compiler at compile time and fix the calling method based on the actual parameter matching or by using implicit type conversion. This is also called as static binding, early binding or compile time polymorphism. Runtime Polymorphism: Method Overriding is a mechanism in which a method in a child class that is already defined in the parent class with the same method signature — same name, arguments, and return type. Method overriding is used to provide the specific implementation of a method which is already provided by its superclass. The method binding is done by the java interpreter at run time and fix the calling method based on the latest implementation in class hierarchy. This is also called as dynamic binding, late binding or run time polymorphism. Inheritance: Inheritance is an important concept of OOP. It is the mechanism in java by which one class is allows inheriting the fields and methods of another class. Inheritance facilitates code reusability to reuse the fields and methods of the existing class. The class from which a new class is created is called as a parent, base or super class and the new class is also called as child, derived or sub class. White spaces, Identifiers, Literals Comments, Separators ,Keywords ▪ White Spaces: White spaces in Java include spaces, tabs, and line breaks (newline characters).White spaces are used to separate tokens (such as keywords, identifiers, and operators) in the source code. They are ignored by the Java compiler and serve to enhance code readability. ▪ Identifiers: Identifiers are used to name variables, classes, methods, and other program elements. Rules for identifiers in Java : Must start with a letter, underscore (_), or dollar sign ($). Can include letters, digits, underscores, and dollar signs. ▪ Literals: Literals are constant values used in Java code. Types of literals in Java include: Integer literals Floating-point literals Character literals String literals Boolean literals Null literal ▪ Comments:Comments are used to add explanatory notes or documentation within the code. In Java, there are two types of comments: Single-line comments, which start with ‘//’ and continue until the end of the line. Multi-line comments (also called block comments), enclosed between ‘ /*’ and , which can span multiple lines. ▪ Separators: Separators are used to separate words, expressions, sentences, blocks etc. ▪ Keywords: these words are already been defined by the language and have a predefined meaning and use. Key words cannot be used as a variable, method, class or interface etc. Introduction to Datatypes of Java In Java, data types specify the type of data that a variable can hold. Java has two main categories of data types: primitive data types and reference data types. Here's an introduction to each of them: Byte, short, int, long, float, double, chars, Boolean Primitive Data Types: Primitive data types represent simple values and are the building blocks of more complex data structures. Java has eight primitive data types: ▪ byte: A 1-byte integer type with a range of -128 to 127. ▪ short: A 2-byte integer type with a range of -32,768 to 32,767. ▪ int: A 4-byte integer type with a range of approximately -2.1 billion to 2.1 billion. ▪ long: An 8-byte integer type with a much larger range than ’int’. ▪ float: A 4-byte floating-point type used for decimal values with limited precision. ▪ double: An 8-byte floating-point type with higher precision than ‘float’ ▪ char: A 2-byte character type representing single Unicode characters. ▪ boolean: A type with only two possible values, ‘true’ or ‘false’ , used for logical operations. ▪ Variable: In programming, a variable is a symbolic name or identifier that represents a value stored in the computer's memory. Variables are used to store and manipulate data within a program. They act as containers for values that can be read, modified, and used in calculations or operations. Variables have a specific data type that defines the kind of data they can hold, such as integers, floating-point numbers, characters, or objects. ▪ Declaring a Variable: Declaring a variable means defining its name and data type, which tells the compiler or interpreter the type of value that will be stored in the variable. ▪ In Java, variable declaration follows a specific syntax: ▪ data_type variable_name; ▪ Dynamic Initialization of Variables: Once a variable is declared, it can be assigned a value through a process called initialization. Dynamic initialization refers to the practice of assigning a value to a variable at runtime, rather than at the time of declaration. This is often done when the initial value of a variable is determined based on some computation or external input. ▪ Example: ▪ int x; ▪ X=10; Introduction to Operators Operators in Java are special symbols or keywords used to perform operations on variables and values. They allow you to manipulate data, perform arithmetic calculations, compare values, and control the flow of your program. Java supports a wide range of operators, which can be categorized into several groups: ▪ Arithmetic Operator ▪ Relational Operator ▪ Logical Operator ▪ Bitwise Operator ▪ Conditional Operator (Ternary) ▪ Assignment Operator Working with Arithmetic, Relational, Logical, Bit wise, Conditional, Assignment operators Arithmetic Operator: Arithmetic operators are used for mathematical calculations. Common arithmetic operators include: Addition(+),Subtraction(- ),Multiplication(*),Division(/),Modulus(%). Relational Operator: Relational operators are used to compare values and determine relationships between them. Common relational operators include: ‘==’ (Equal to), ‘!=’ (Not Equal to), ‘’(Greater than), ‘=’ (Greater than or equal to). Logical Operator: Logical operators are used to perform logical operations on boolean values (true or false). Common logical operators include: ‘&&’ (Logical AND) ,”|| ” Logical OR, ‘!’ (Logical NOT). Bitwise Operator: Bitwise operators perform operations at the bit level. Common bitwise operators include: ‘&’ (Bitwise AND),’|’ (Bitwise OR), ‘^’ (Bitwise XOR), ‘~’ (Bitwise NOT), ‘’ (Right shift). Conditional Operator (Ternary) : The conditional operator (‘’? :’) is used for the shorthand way of writing am ‘if-else’ statement. It evaluate a Boolean expression and return one of two values based on the result. Assignment Operator: Assignment operators are used to assign values to variables. Common assignment operators include: ’=’ (Assignment), ’+=’ (Add and Assign), ’-=’ (Subtract and Assign), ’*=’ (Multiply and Assign), ’/=’ (Divide and Assign). What is Array?, Initialization of Arrays An array in Java is a data structure that allows you to store multiple values of the same data type under a single variable name. Arrays are used to store collections of elements, and each element can be accessed by its index (position) within the array. Arrays provide a convenient way to work with lists or sequences of data. Here's an introduction to the initialization of arrays in Java: Declaring an Array: To declare an array in Java, you specify the data type of the elements it will hold, followed by the array's name and square brackets ‘[ ]’. Here's the basic syntax: data_type[] array_name; Initializing an Array: Once you've declared an array, you need to initialize it. Initialization involves creating the array and specifying its size (the number of elements it can hold). There are several ways to initialize arrays in Java: Using “new” Keyword: Int[ ] numbers = new int; Initializing with Values: int[ ] numbers ={ 1,2,3,4,5}; Understanding Types of Arrays two-dimensional array is a list of list of like typed variables or an array of array. The general form of a one-dimensional array declaration is [access_specifier] type var-name[ ][ ]; Eg. publicint m1[][]=new int This will declare a 2D array of int to hold 3 rows and 3 columns. Introduction to Control Statements Control statements in Java are constructs that enable you to control the flow of your program's execution. They allow you to make decisions, repeat actions, and execute code based on specific conditions. Control statements are a fundamental part of programming, helping you create dynamic and interactive applications. Java provides several types of control statements: ▪ Conditional Statements: I. if Statement II. switch Statement ▪ Looping Statements: I. for Loop II. while Loop III. do-while Loop ▪ Jump Statements: I. break Statement II. continue Statement III. return Statement Working with Selection Statements-All forms of if & Switch Simple if Statement: The simple if statement is used when you want to execute a block of code only if a single condition is true. Syntax: if (condition) { // Code to execute if the condition is true } if-else Statement: The if-else statement allows you to specify an alternative block of code to execute if the condition is false. Syntax: if (condition) { // Code to execute if the condition is true } else { // Code to execute if the condition is false } if-else if-else Statement: Use if-else if-else when you have multiple conditions to test, and you want to execute different blocks of code depending on which condition is true. Syntax: if (condition1) { // Code to execute if condition1 is true } else if (condition2) { // Code to execute if condition2 is true } else { // Code to execute if none of the conditions are true } Switch Statement: The switch statement allows you to select one of many code blocks to execute based on the value of an expression. It's particularly useful when you have a value that can match several cases. Syntax: switch (expression) { case value1: // Code to execute if expression equals value1 break; case value2: // Code to execute if expression equals value2 break; //... default: // Code to execute if expression doesn't match any case } Introduction to Iterative Statements Iterative statements in Java, also known as loops, are control flow constructs that allow you to repeatedly execute a block of code as long as a certain condition is met. They are fundamental for automating repetitive tasks and processing collections of data. Java provides three main types of iterative statements: ❑ for loop ❑ while loop ❑ do-while loop Working with while, do-while, for, for each statements for Loop: The for loop is commonly used when you know beforehand how many times you want to execute a block of code. It has three components: initialization, condition, and update expression Syntax: for (initialization; condition; update) { // Code to be executed repeatedly } while Loop: The while loop is used when you want to repeatedly execute a block of code as long as a specified condition is true. The condition is checked before each iteration. Syntax: while (condition) { // Code to be executed repeatedly } do-while Loop: The do-while loop is similar to the while loop but guarantees that the code block is executed at least once before checking the condition. The condition is checked after each iteration Syntax: do { // Code to be executed repeatedly } while (condition); Introduction to Jump Statements Jump statements in Java are used to control the flow of program execution by transferring control to another part of the program. These statements allow you to exit loops prematurely, skip iterations, or transfer control to a different location in your code. Java provides three main jump statements: ❑ break statement ❑ continue statement ❑ return statement Working with break, continue and return Statements break Statement: The break statement is used to exit a loop prematurely. It can be used within a for, while, or do-while loop to terminate the loop and continue execution after the loop. Example: for (int i = 0; i < 5; i++) { if (i == 3) { break; // Exit the loop when i equals 3 } System.out.println("Iteration " + i); } continue Statement: The continue statement is used to skip the current iteration of a loop and proceed to the next iteration. It can be used within a for, while, or do-while loop. Example: for (int i = 0; i < 5; i++) { if (i == 2) { continue; // Skip the iteration when i equals 2 } System.out.println("Iteration " + i); } return Statement: The return statement is used to exit from a method and return a value to the caller. It can be used in methods that have a return type other than void Example: public int add(int a, int b) { int result = a + b; return result; // Exit the method and return 'result' to the caller }