This presentation starts with the history and evolution of Java followed by OOP paradigms. Then the data types, variables and arrays were discussed. After that the classes and objects were introduced
Java was developed in 1991 by James Gosling, Mike Sheridan, and Patrick Naughton at Sun Microsystems. It was originally called "Oak" but was renamed to Java in 1995. Java was created to be platform independent, allowing programs written in Java to run on any device without modification, unlike other languages at the time. This platform independence is known as "write once, run anywhere." Java was later acquired by Oracle Corporation in 2010 and continues to be updated with new versions, the most recent being Java SE9 released in September 2017.
The document discusses key features of ECMAScript 6 (ES6), including:
- Default parameters, template literals, multi-line strings, spread operator, and enhanced object literals which add concise syntaxes.
- Arrow functions which provide a shorter syntax for writing anonymous functions.
- Block-scoped constructs like let and const that add block scoping to variables and constants.
- Classes which provide a cleaner way to define constructor functions and objects.
- Hoisting differences between function declarations and class declarations.
- Using ES6 today by compiling it to ES5 using a tool like Babel.
The document discusses key concepts in Java including classes, objects, methods, and command line arguments. A class defines common properties and behaviors for objects through fields and methods. Objects are instantiated from classes and can access fields and methods using dot notation. Command line arguments allow passing data into a Java application and are accessed through the args parameter in the main method.
The document provides an introduction and history of Java, outlining how it was developed in the 1990s as a platform-independent language by James Gosling at Sun Microsystems, and discusses some key advantages of Java like being object-oriented, portable, robust, and having built-in support for security and multithreading. It also describes the Java Development Kit (JDK) which contains tools for developing Java programs and the Java Runtime Environment (JRE) which allows running of Java applications and includes the Java Virtual Machine.
The access modifiers in Java specify the visibility or scope of classes, fields, methods, and constructors. There are four access modifiers: private (visible only within the class), default (visible within the package), protected (visible within the package and subclasses), and public (visible everywhere). Access modifiers determine where classes, fields, methods, and constructors declared with the modifiers can be accessed.
Introduction to Java Programming Languagejaimefrozr
The document provides an introduction and history of the Java programming language. It discusses that Java was originally developed in 1991 by Sun Microsystems to be portable for consumer electronic devices. The document then summarizes the key capabilities of Java including being a general purpose language that can develop robust applications for desktops, servers, and mobile devices. It also outlines the Java language specifications, application programming interface containing predefined classes, and development tools available. Finally, it explains how Java's use of byte code and the Java Virtual Machine allows it to be highly portable across different operating systems.
This Edureka Java Tutorial will help you in understanding the various fundamentals of Java in detail with examples. Below are the topics covered in this tutorial:
1) Introduction to Java
2) Why learn Java?
3) Features of Java
4) How does Java work?
5) Data types in Java
6) Operators in Java
7) Control Statements in Java
8) Arrays in Java
9) Object Oriented Concepts in Java
The document provides an overview of Java applets, including:
1. An applet is a Java program that can be embedded in an HTML page and runs in web browsers.
2. Advantages of applets include being cross-platform, supported by most browsers, and cached for faster loading.
3. Disadvantages include requiring the Java plugin and JVM, and being slower to load than HTML.
Hibernate is an object-relational mapping tool that allows developers to more easily write applications that interact with relational databases. It does this by allowing developers to map Java classes to database tables and columns, so that developers can interact with data through Java objects rather than directly with SQL statements. Hibernate handles the conversion between Java objects and database rows behind the scenes. Some key benefits of using Hibernate include faster data retrieval, avoiding manual database connection management, and easier handling of database schema changes.
String Handling, Inheritance, Packages and InterfacesPrabu U
The presentation starts with string handling. Then the concepts of inheritance is detailed. Finally the concepts of packages and interfaces are detailed.
This document provides an overview of Java and getting started with Java programming. It discusses Java's key features like being simple, network savvy, secure, and portable. It also covers Java basics like the Java logo, sample programs, how a Java program flows from source to execution. It explains key Java concepts like the JDK, JRE, JVM, and flavors of Java. It provides guidance on writing Java programs using Notepad or an IDE, and references books for further learning Java.
Java Server Pages (JSP) allow Java code to be embedded within HTML pages to create dynamic web content. JSP pages are translated into servlets by the web server. This involves compiling the JSP page into a Java servlet class that generates the HTML response. The servlet handles each request by executing the jspService() method and produces dynamic content which is returned to the client browser.
This keyword is a reference variable that refer the current object in java.
This keyword can be used for call current class constructor.
http://www.tutorial4us.com/java/java-this-keyword
This document provides an introduction and overview of the Java programming language. It discusses that Java was developed by Sun Microsystems in the 1990s as a general-purpose, object-oriented language designed for easy web and internet applications. The key principles of object-oriented programming like encapsulation, inheritance, and polymorphism are explained. Characteristics of Java like being simple, secure, portable, and having good performance are highlighted. A brief history of Java's development is also presented.
This document discusses Java collections framework and various collection classes like ArrayList, LinkedList, HashSet, HashMap etc. It provides definitions and examples of commonly used collection interfaces like List, Set and Map. It explains key features of different collection classes like order, duplicates allowed, synchronization etc. Iterators and generic types are also covered with examples to iterate and create typed collection classes.
An interface in Java is a blueprint of a class that defines static constants and abstract methods. Interfaces are implemented by classes where they inherit the properties and must define the body of the abstract methods. Key points are:
- Interfaces can only contain abstract methods and static constants, not method bodies.
- Classes implement interfaces to inherit the properties and must define the abstract method bodies.
- An interface can extend other interfaces and a class can implement multiple interfaces.
Interfaces define methods that classes can implement. Classes implementing interfaces must define all interface methods. Interfaces can extend other interfaces, requiring implementing classes to define inherited methods as well. Interface variables are implicitly public, static, and final. A class can implement multiple interfaces and override methods with the same name across interfaces. Partial interface implementation requires the class to be abstract.
Frameworks are large prewritten code to which you add your own code to solve a problem in a specific domain.
You make use of a framework by calling its methods,inheritance,and supplying “call-backs” listeners.
Spring is the most popular application development framework for enterprise Java™.
Millions of developers use Spring to create high performing, easily testable, reusable code without any lock-in.
This document provides an introduction and overview of the Java programming language. It discusses that Java was developed by James Gosling in 1990 at Sun Microsystems to be a simple, secure, object-oriented language for developing applications on the internet. The document then covers Java's history, key features like platform independence and object orientation, and how Java programs are executed using the Java Virtual Machine. It also summarizes the different modules of Java - JSE, JEE, and JME - and provides a basic example Java program.
JDK stand for java development kit.
JVM stands for Java Virtual Machine.
JRE is the responsible unit to run the java program.
JIT stands for Just In Time compiler.
Servlets are Java programs that run on a web or application server and act as a middle layer between a request coming from a web browser or other HTTP client and databases or applications on the HTTP server. Servlets receive HTTP requests and return HTTP responses by accepting request parameters, generating dynamic content, accessing databases, and performing network communications using Java. Servlets are commonly used to add dynamic content to web pages and to access backend databases. The lifecycle of a servlet involves initialization, servicing client requests, and destruction. Common servlet APIs include classes for handling HTTP requests and responses, reading request parameters, using cookies and sessions.
Java is a programming language that compiles code to bytecode that runs on a Java Virtual Machine (JVM). The JVM is an abstraction layer that executes bytecode similarly across operating systems. It includes components like the bytecode verifier, class loader, execution engine, garbage collector, and security manager. The JVM allows Java to be platform independent and "write once, run anywhere".
The document discusses several core Java concepts including:
1) Comments in Java code can be single-line or multiline javadoc comments.
2) Classes are fundamental in Java and describe data objects and methods that can be applied to objects.
3) Variables and methods have scopes determined by curly braces and a variable is only available within its scope.
This document provides an overview of the Java Collections Framework. It discusses core collection interfaces like List, Set, and Map and their common implementations like ArrayList, HashSet, and HashMap. It also covers sorting collections with Comparable and Comparator, searching collections with binary search, and using utility methods in the Collections class. Examples are provided to illustrate usage of various collection classes and methods.
The document provides an introduction to the Java programming language. It discusses that Java was originally called Oak and developed by Sun Microsystems in 1991. It can be used to create both applications and applets. The source code is compiled into bytecode, which is then interpreted by the Java Virtual Machine (JVM) to execute programs. Key features of Java include being simple, secure, portable, object-oriented, robust, and multi-threaded. The document also covers concepts such as packages, interfaces, inheritance, method overloading/overriding, and multithreading.
Java is a widely used, general-purpose programming language that is object-oriented, platform-independent, high-performance, and secure. It has remained popular for many years due to its robust features like automatic memory management, exception handling, and built-in security. Java code is compiled to bytecode that runs on a Java Virtual Machine, providing platform independence so that programs can run on any device.
Java was created in the early 1990s and first released in 1995. It has since gone through many versions with new features and improvements. Some key releases included JDK 1.0 in 1996, Java 2 in 1998 which introduced Swing, and Java 5 in 2004 which added generics. More recent versions have focused on modularity, lambda expressions, and memory management. With each new release, Java continues to evolve by bringing powerful new features to simplify development.
Hibernate is an object-relational mapping tool that allows developers to more easily write applications that interact with relational databases. It does this by allowing developers to map Java classes to database tables and columns, so that developers can interact with data through Java objects rather than directly with SQL statements. Hibernate handles the conversion between Java objects and database rows behind the scenes. Some key benefits of using Hibernate include faster data retrieval, avoiding manual database connection management, and easier handling of database schema changes.
String Handling, Inheritance, Packages and InterfacesPrabu U
The presentation starts with string handling. Then the concepts of inheritance is detailed. Finally the concepts of packages and interfaces are detailed.
This document provides an overview of Java and getting started with Java programming. It discusses Java's key features like being simple, network savvy, secure, and portable. It also covers Java basics like the Java logo, sample programs, how a Java program flows from source to execution. It explains key Java concepts like the JDK, JRE, JVM, and flavors of Java. It provides guidance on writing Java programs using Notepad or an IDE, and references books for further learning Java.
Java Server Pages (JSP) allow Java code to be embedded within HTML pages to create dynamic web content. JSP pages are translated into servlets by the web server. This involves compiling the JSP page into a Java servlet class that generates the HTML response. The servlet handles each request by executing the jspService() method and produces dynamic content which is returned to the client browser.
This keyword is a reference variable that refer the current object in java.
This keyword can be used for call current class constructor.
http://www.tutorial4us.com/java/java-this-keyword
This document provides an introduction and overview of the Java programming language. It discusses that Java was developed by Sun Microsystems in the 1990s as a general-purpose, object-oriented language designed for easy web and internet applications. The key principles of object-oriented programming like encapsulation, inheritance, and polymorphism are explained. Characteristics of Java like being simple, secure, portable, and having good performance are highlighted. A brief history of Java's development is also presented.
This document discusses Java collections framework and various collection classes like ArrayList, LinkedList, HashSet, HashMap etc. It provides definitions and examples of commonly used collection interfaces like List, Set and Map. It explains key features of different collection classes like order, duplicates allowed, synchronization etc. Iterators and generic types are also covered with examples to iterate and create typed collection classes.
An interface in Java is a blueprint of a class that defines static constants and abstract methods. Interfaces are implemented by classes where they inherit the properties and must define the body of the abstract methods. Key points are:
- Interfaces can only contain abstract methods and static constants, not method bodies.
- Classes implement interfaces to inherit the properties and must define the abstract method bodies.
- An interface can extend other interfaces and a class can implement multiple interfaces.
Interfaces define methods that classes can implement. Classes implementing interfaces must define all interface methods. Interfaces can extend other interfaces, requiring implementing classes to define inherited methods as well. Interface variables are implicitly public, static, and final. A class can implement multiple interfaces and override methods with the same name across interfaces. Partial interface implementation requires the class to be abstract.
Frameworks are large prewritten code to which you add your own code to solve a problem in a specific domain.
You make use of a framework by calling its methods,inheritance,and supplying “call-backs” listeners.
Spring is the most popular application development framework for enterprise Java™.
Millions of developers use Spring to create high performing, easily testable, reusable code without any lock-in.
This document provides an introduction and overview of the Java programming language. It discusses that Java was developed by James Gosling in 1990 at Sun Microsystems to be a simple, secure, object-oriented language for developing applications on the internet. The document then covers Java's history, key features like platform independence and object orientation, and how Java programs are executed using the Java Virtual Machine. It also summarizes the different modules of Java - JSE, JEE, and JME - and provides a basic example Java program.
JDK stand for java development kit.
JVM stands for Java Virtual Machine.
JRE is the responsible unit to run the java program.
JIT stands for Just In Time compiler.
Servlets are Java programs that run on a web or application server and act as a middle layer between a request coming from a web browser or other HTTP client and databases or applications on the HTTP server. Servlets receive HTTP requests and return HTTP responses by accepting request parameters, generating dynamic content, accessing databases, and performing network communications using Java. Servlets are commonly used to add dynamic content to web pages and to access backend databases. The lifecycle of a servlet involves initialization, servicing client requests, and destruction. Common servlet APIs include classes for handling HTTP requests and responses, reading request parameters, using cookies and sessions.
Java is a programming language that compiles code to bytecode that runs on a Java Virtual Machine (JVM). The JVM is an abstraction layer that executes bytecode similarly across operating systems. It includes components like the bytecode verifier, class loader, execution engine, garbage collector, and security manager. The JVM allows Java to be platform independent and "write once, run anywhere".
The document discusses several core Java concepts including:
1) Comments in Java code can be single-line or multiline javadoc comments.
2) Classes are fundamental in Java and describe data objects and methods that can be applied to objects.
3) Variables and methods have scopes determined by curly braces and a variable is only available within its scope.
This document provides an overview of the Java Collections Framework. It discusses core collection interfaces like List, Set, and Map and their common implementations like ArrayList, HashSet, and HashMap. It also covers sorting collections with Comparable and Comparator, searching collections with binary search, and using utility methods in the Collections class. Examples are provided to illustrate usage of various collection classes and methods.
The document provides an introduction to the Java programming language. It discusses that Java was originally called Oak and developed by Sun Microsystems in 1991. It can be used to create both applications and applets. The source code is compiled into bytecode, which is then interpreted by the Java Virtual Machine (JVM) to execute programs. Key features of Java include being simple, secure, portable, object-oriented, robust, and multi-threaded. The document also covers concepts such as packages, interfaces, inheritance, method overloading/overriding, and multithreading.
Java is a widely used, general-purpose programming language that is object-oriented, platform-independent, high-performance, and secure. It has remained popular for many years due to its robust features like automatic memory management, exception handling, and built-in security. Java code is compiled to bytecode that runs on a Java Virtual Machine, providing platform independence so that programs can run on any device.
Java was created in the early 1990s and first released in 1995. It has since gone through many versions with new features and improvements. Some key releases included JDK 1.0 in 1996, Java 2 in 1998 which introduced Swing, and Java 5 in 2004 which added generics. More recent versions have focused on modularity, lambda expressions, and memory management. With each new release, Java continues to evolve by bringing powerful new features to simplify development.
Java was originally developed by James Gosling at Sun Microsystems in 1995 and released as a core component of Sun's Java platform. Some key updates and features of newer Java versions include lambda expressions and streams in Java 8, which improved efficiency. Java 9 added a lightweight JSON API and HTTP/2 client, while Java updates focused on performance improvements and modularizing the JDK source code. The latest versions of Java aimed to simplify development through new language features and tools while maintaining security and portability across platforms.
This document provides an introduction to object oriented programming in Java. It outlines the course objectives which are to learn Java basics, object oriented principles, Java APIs, exception handling, files, threads, applets and swings. It discusses key characteristics of Java including being portable, object oriented and having automatic memory management. It also provides an overview of Java environments and tools, and includes an example "Hello World" Java program.
Java was developed in 1991 at Sun Microsystems by James Gosling and others to be a platform-independent object-oriented programming language. It was initially called Oak but renamed to Java in 1995. Java compiles code to bytecode that runs on a Java Virtual Machine (JVM) allowing programs to run on any device with a JVM. Major features of Java include being object-oriented, portable, robust, secure, multithreaded, and dynamic. Java has evolved through several versions and is widely used for internet applications and enterprise software.
This document provides an overview of Java for Perl programmers. It describes Java as an object-oriented language with a virtual machine that allows applications to run on any system with a JVM. While Java and Perl have similar performance, Java may be preferable for building cross-platform GUIs, client applications like applets, or applications that interact heavily with databases. Perl remains a better choice for procedural or hybrid approaches and for integrating with Unix shells. Programmers should understand both languages to choose the best for each project.
This document provides an overview of Java for Perl programmers. It describes Java as an object-oriented language with a virtual machine that allows applications to run on any system with a JVM. While Java and Perl have similar performance, Java may be preferable for building cross-platform GUIs, client applications like applets, or applications that interact heavily with databases. Perl remains a better choice for procedural or hybrid approaches and for integrating with Unix shells. Programmers should understand both languages to choose the best for each project.
Excellence Technology offers java training in mohali we offer training on Core java and Advance Java Training in Mohali and we also provide advance java training in mohali. we are into the Java development since 2015. We have well knitted team of Java Expert Level Programmers who would train you during the course of your Java Industrial Training to get you familiar with real working environment. With new technology getting launched every few months, students are confused on what technology they should learn. Here is why we feel JAVA is the best and you should definitely go for Advance JAVA Training.Excellence Technology is the best java training institute in Chandigarh.
In this Java tutorial, we are going to learn about java evolution, development of Java language, java features, Java frameworks, and Java support systems. Java tutorial covers various development tools, classes in Java programming, and Java methods which comprises the Java environment. This tutorial on java will help you get a quick insight into the Java programming language and thus, help you learn java language.
The document provides an introduction to Java programming language. It discusses that Java was originally developed by James Gosling at Sun Microsystems in 1991 and was named Oak. It was later renamed to Java in 1995. The document also describes Java features such as it is a purely object-oriented language, platform independent, secure, robust, portable, and supports multithreading.
This document provides an introduction to an Object Oriented Programming in Java course. The course will cover core computer science concepts needed to create modern Java applications, including object oriented principles like encapsulation, inheritance, polymorphism. It will also cover Java APIs, exception handling, files, threads, applets, and Swing GUI programming. The course is divided into 5 units covering Java basics, inheritance, data structures, exception handling, and GUI programming.
This document provides an overview of the different versions of Java Development Kits (JDKs) and Java platforms, including JDK 1.0 through 1.4, Java 2 Standard Edition (J2SE) 1.2 through 1.4, Java 2 Enterprise Edition (J2EE), and Java 2 Micro Edition (J2ME). It describes the major additions and improvements in each release, such as new APIs, features, and deprecated classes. It also discusses the differences between the Java language and platforms. Finally, it provides support for the statement that while the Java language specification has not changed dramatically since JDK 1.1, the Java APIs have improved significantly.
JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation. It was developed by James Gosling and Patrick Naughton. 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.
java tutorial for beginner - Free DownloadTIB Academy
TIB Academy offering Java tutorial with fundamental for beginner This Java Tutorial going to explain about the fundamentals of Core Java Concepts, you can download Java tutorial as PPT for free of cost
This document provides an introduction to object-oriented programming using Java. It outlines the course objectives, which are to learn Java basics, inheritance, data structures, exception handling, and GUI programming. It then discusses key aspects of Java like its history, differences from C and C++, characteristics, environment, and architecture neutrality. Finally, it demonstrates a simple "Hello World" Java program and its execution.
This document provides an introduction to Java fundamentals and object-oriented programming concepts. It outlines the course objectives which include learning Java features, OOP principles, and how to program using the Java API. The document then discusses Java basics like its history and importance, differences between Java and C/C++, Java characteristics, environment, and execution model. It also demonstrates a simple "Hello World" Java program and how to run it.
This document provides an introduction to Java fundamentals and object-oriented programming concepts. It outlines the course objectives which include learning Java features, OOP principles, and how to program using the Java API. The document then discusses Java basics like its history and importance, differences between Java and C/C++, Java characteristics, environment, and execution model. It also demonstrates a simple "Hello World" Java program and how to run it.
This document provides an introduction to Java fundamentals and object-oriented programming concepts. It outlines the course objectives which include learning Java features, OOP principles, and how to program using the Java API. The document then discusses Java basics like its history and importance, differences between Java and C/C++, Java characteristics, environment, and execution model. It also includes an example "Hello World" Java program and how to run it.
This document provides an overview of the Java programming language. It discusses the basic structure of a Java program, including documentation, packages, imports, classes, and the main method. It also covers the evolution of Java from C and C++, the Java Virtual Machine (JVM), platform independence, and popular Java editors like NetBeans and Eclipse. Popular uses of Java include web applications, mobile apps, and enterprise software.
Concurrency and Parallelism, Asynchronous Programming, Network ProgrammingPrabu U
The presentation starts with concurrency and parallelism. Then the concepts of reactive programming is covered. Finally network programming is detailed
File Input/output, Database Access, Data Analysis with PandasPrabu U
The presentation starts with File Input and Output. Then the concepts of Database Access is detailed. Atlast the concepts data analysis with Pandas is covered
This document provides an overview of arrays and operations on arrays using NumPy. It discusses creating arrays, mathematical operations on arrays like basic operations, squaring arrays, indexing and slicing arrays, and shape manipulation. Mathematical operations covered include conditional operations and matrix multiplication. Indexing and slicing cover selecting single elements, counting backwards with negative indexes, and combining positive and negative indexes. Shape manipulation discusses changing an array's shape, size, combining arrays, splitting arrays, and repeating arrays.
The document provides an introduction to XML including its structure, elements, attributes, and namespaces. It discusses XML declarations, document type declarations, elements, attributes, character data, comments, processing instructions, content models, and the handling of whitespace in XML documents. It also covers XML namespaces, default and explicit namespace declarations, and the scope of namespaces. Finally, it discusses the structure of document type definitions including elements, attributes, entities, and directives.
Introduction to Web Services, UDDI, SOAP, WSDL, Web Service Architecture, Developing and deploying web services.
Ajax – Improving web page performance using Ajax, Programming in Ajax.
The document discusses XML (eXtensible Markup Language) and related technologies. It begins with an introduction to XML, describing it as a means of structuring data. It then covers XML revolutions, basics, defining XML documents using DTDs and XML Schema, and technologies related to XML like XPath and XSLT. Key topics include XML design goals, roles of XML, XML document structure, element rules and types in DTDs, attributes, entities, and data types in XML Schema. The document provides information on core XML concepts in a technical yet concise manner.
Internet Principles and Components, Client-Side ProgrammingPrabu U
Internet Principles and Components: History of the Internet and World Wide Web – HTML - Protocols – HTTP, SMTP, POP3, MIME, and IMAP. Domain Name Server, Web Browsers and Web Servers. HTML- Style Sheets- CSS- Introduction to Cascading Style Sheets-Rule- Features- Selectors- Attributes.
Client-Side Programming: The JavaScript Language- JavaScript in Perspective-Syntax-Variables and Data Types- Statements- Operators- Literals- Functions- Objects- Arrays-Built-in Objects- JavaScript Debuggers and Regular Expression.
This document provides an overview of operations management, marketing management, and financial management. It discusses topics such as production planning and control, quality control, inventory control, pricing strategies, product development, distribution channels, and promotional activities. Key points covered include the importance of customer orientation, integrating marketing mix elements, using techniques like critical path analysis and linear programming in operations, and balancing costs and market demands in pricing decisions.
This document provides an overview of management concepts including:
- The nature and importance of management including its functions such as decision making, organizing, staffing, etc.
- The development of management thought from classical to modern approaches.
- The importance of ethical and environmental foundations for management including managing social responsibility and value systems.
- Key philosophies of management and how they differ between organizations.
This document discusses replacement and maintenance analysis, including determining the economic life of assets. It provides examples of calculating the economic life of equipment using total cost when interest is 0% and 12%. It also discusses replacement of existing assets, types of maintenance, and a simple probabilistic model for items that fail completely. Optimal replacement policies are determined by comparing individual and group replacement costs. The document also covers several methods of depreciation, including straight-line depreciation calculation examples.
This document provides an overview of engineering economics and elementary economic analysis concepts. It discusses the definition and goals of economics, including the production and distribution of goods and services for human welfare. Key points covered include the law of supply and demand, factors that influence supply and demand, costs and revenues, break-even analysis, and the profit-volume ratio. Elementary economic analysis is introduced as a way to make economic decisions by considering factors like price, transportation costs, availability, and quality when evaluating alternatives. Examples are also provided to illustrate basic economic analysis concepts.
This document provides an overview of engineering economics and management concepts across 4 sections:
1. It introduces microeconomics, macroeconomics, economic and technical decisions, demand and supply concepts, and break-even analysis.
2. It defines microeconomics as the study of particular markets and segments of the economy like consumer behavior and firm theory. It also outlines characteristics, scope, and importance of microeconomics.
3. It states that macroeconomics deals with aggregates like national income rather than individual quantities. It discusses key issues in macroeconomics like economic growth, business cycles, and unemployment.
4. It describes the process of managerial decision making and sources of uncertainty. It also distingu
This document discusses files and file operations in C programming. It covers opening, closing, reading from, and writing to files. Key points include:
- There are different modes for opening files, such as read ("r"), write ("w"), and append ("a").
- Common file functions include fopen() to open a file, fclose() to close it, fread() and fwrite() for reading and writing data, and fgetc() and fputc() for characters.
- Files can be accessed sequentially from the beginning or randomly by using functions like fseek() and ftell() to set and get the file position.
- Command line arguments allow passing parameters to a
The document discusses structures in C programming. It defines structures as a way to pack together logically related data items of different types. Some key points:
- Structures allow defining custom data types that group together members of integer, float, character, and other standard types.
- Structure variables are declared and members accessed using the dot operator. Arrays of structures can also be defined.
- Structures can be initialized in various ways and passed to functions by value or by reference.
- Nested structures, where one structure is defined as a member of another, are also supported.
The document also covers arrays of structures, passing structures to functions, user-defined data types using typedef and enums,
Decision Making Statements, Arrays, StringsPrabu U
The document discusses various decision making and looping statements in C programming language. It describes if, if-else, nested if, switch statements for decision making. It also covers while, for, do-while and nested loops for looping/iteration. Examples are provided for each statement to illustrate their syntax and usage. Arrays, functions, recursion and passing arrays to functions are also mentioned as topics to be covered.
This paper proposes a shoulder inverse kinematics (IK) technique. Shoulder complex is comprised of the sternum, clavicle, ribs, scapula, humerus, and four joints.
Raish Khanji GTU 8th sem Internship Report.pdfRaishKhanji
This report details the practical experiences gained during an internship at Indo German Tool
Room, Ahmedabad. The internship provided hands-on training in various manufacturing technologies, encompassing both conventional and advanced techniques. Significant emphasis was placed on machining processes, including operation and fundamental
understanding of lathe and milling machines. Furthermore, the internship incorporated
modern welding technology, notably through the application of an Augmented Reality (AR)
simulator, offering a safe and effective environment for skill development. Exposure to
industrial automation was achieved through practical exercises in Programmable Logic Controllers (PLCs) using Siemens TIA software and direct operation of industrial robots
utilizing teach pendants. The principles and practical aspects of Computer Numerical Control
(CNC) technology were also explored. Complementing these manufacturing processes, the
internship included extensive application of SolidWorks software for design and modeling tasks. This comprehensive practical training has provided a foundational understanding of
key aspects of modern manufacturing and design, enhancing the technical proficiency and readiness for future engineering endeavors.
π0.5: a Vision-Language-Action Model with Open-World GeneralizationNABLAS株式会社
今回の資料「Transfusion / π0 / π0.5」は、画像・言語・アクションを統合するロボット基盤モデルについて紹介しています。
拡散×自己回帰を融合したTransformerをベースに、π0.5ではオープンワールドでの推論・計画も可能に。
This presentation introduces robot foundation models that integrate vision, language, and action.
Built on a Transformer combining diffusion and autoregression, π0.5 enables reasoning and planning in open-world settings.
The Fluke 925 is a vane anemometer, a handheld device designed to measure wind speed, air flow (volume), and temperature. It features a separate sensor and display unit, allowing greater flexibility and ease of use in tight or hard-to-reach spaces. The Fluke 925 is particularly suitable for HVAC (heating, ventilation, and air conditioning) maintenance in both residential and commercial buildings, offering a durable and cost-effective solution for routine airflow diagnostics.
International Journal of Distributed and Parallel systems (IJDPS)samueljackson3773
The growth of Internet and other web technologies requires the development of new
algorithms and architectures for parallel and distributed computing. International journal of
Distributed and parallel systems is a bimonthly open access peer-reviewed journal aims to
publish high quality scientific papers arising from original research and development from
the international community in the areas of parallel and distributed systems. IJDPS serves
as a platform for engineers and researchers to present new ideas and system technology,
with an interactive and friendly, but strongly professional atmosphere.
In tube drawing process, a tube is pulled out through a die and a plug to reduce its diameter and thickness as per the requirement. Dimensional accuracy of cold drawn tubes plays a vital role in the further quality of end products and controlling rejection in manufacturing processes of these end products. Springback phenomenon is the elastic strain recovery after removal of forming loads, causes geometrical inaccuracies in drawn tubes. Further, this leads to difficulty in achieving close dimensional tolerances. In the present work springback of EN 8 D tube material is studied for various cold drawing parameters. The process parameters in this work include die semi-angle, land width and drawing speed. The experimentation is done using Taguchi’s L36 orthogonal array, and then optimization is done in data analysis software Minitab 17. The results of ANOVA shows that 15 degrees die semi-angle,5 mm land width and 6 m/min drawing speed yields least springback. Furthermore, optimization algorithms named Particle Swarm Optimization (PSO), Simulated Annealing (SA) and Genetic Algorithm (GA) are applied which shows that 15 degrees die semi-angle, 10 mm land width and 8 m/min drawing speed results in minimal springback with almost 10.5 % improvement. Finally, the results of experimentation are validated with Finite Element Analysis technique using ANSYS.
☁️ GDG Cloud Munich: Build With AI Workshop - Introduction to Vertex AI! ☁️
Join us for an exciting #BuildWithAi workshop on the 28th of April, 2025 at the Google Office in Munich!
Dive into the world of AI with our "Introduction to Vertex AI" session, presented by Google Cloud expert Randy Gupta.
Analysis of reinforced concrete deep beam is based on simplified approximate method due to the complexity of the exact analysis. The complexity is due to a number of parameters affecting its response. To evaluate some of this parameters, finite element study of the structural behavior of the reinforced self-compacting concrete deep beam was carried out using Abaqus finite element modeling tool. The model was validated against experimental data from the literature. The parametric effects of varied concrete compressive strength, vertical web reinforcement ratio and horizontal web reinforcement ratio on the beam were tested on eight (8) different specimens under four points loads. The results of the validation work showed good agreement with the experimental studies. The parametric study revealed that the concrete compressive strength most significantly influenced the specimens’ response with the average of 41.1% and 49 % increment in the diagonal cracking and ultimate load respectively due to doubling of concrete compressive strength. Although the increase in horizontal web reinforcement ratio from 0.31 % to 0.63 % lead to average of 6.24 % increment on the diagonal cracking load, it does not influence the ultimate strength and the load-deflection response of the beams. Similar variation in vertical web reinforcement ratio leads to an average of 2.4 % and 15 % increment in cracking and ultimate load respectively with no appreciable effect on the load-deflection response.
Concept of Problem Solving, Introduction to Algorithms, Characteristics of Algorithms, Introduction to Data Structure, Data Structure Classification (Linear and Non-linear, Static and Dynamic, Persistent and Ephemeral data structures), Time complexity and Space complexity, Asymptotic Notation - The Big-O, Omega and Theta notation, Algorithmic upper bounds, lower bounds, Best, Worst and Average case analysis of an Algorithm, Abstract Data Types (ADT)
Fluid mechanics is the branch of physics concerned with the mechanics of fluids (liquids, gases, and plasmas) and the forces on them. Originally applied to water (hydromechanics), it found applications in a wide range of disciplines, including mechanical, aerospace, civil, chemical, and biomedical engineering, as well as geophysics, oceanography, meteorology, astrophysics, and biology.
It can be divided into fluid statics, the study of various fluids at rest, and fluid dynamics.
Fluid statics, also known as hydrostatics, is the study of fluids at rest, specifically when there's no relative motion between fluid particles. It focuses on the conditions under which fluids are in stable equilibrium and doesn't involve fluid motion.
Fluid kinematics is the branch of fluid mechanics that focuses on describing and analyzing the motion of fluids, such as liquids and gases, without considering the forces that cause the motion. It deals with the geometrical and temporal aspects of fluid flow, including velocity and acceleration. Fluid dynamics, on the other hand, considers the forces acting on the fluid.
Fluid dynamics is the study of the effect of forces on fluid motion. It is a branch of continuum mechanics, a subject which models matter without using the information that it is made out of atoms; that is, it models matter from a macroscopic viewpoint rather than from microscopic.
Fluid mechanics, especially fluid dynamics, is an active field of research, typically mathematically complex. Many problems are partly or wholly unsolved and are best addressed by numerical methods, typically using computers. A modern discipline, called computational fluid dynamics (CFD), is devoted to this approach. Particle image velocimetry, an experimental method for visualizing and analyzing fluid flow, also takes advantage of the highly visual nature of fluid flow.
Fundamentally, every fluid mechanical system is assumed to obey the basic laws :
Conservation of mass
Conservation of energy
Conservation of momentum
The continuum assumption
For example, the assumption that mass is conserved means that for any fixed control volume (for example, a spherical volume)—enclosed by a control surface—the rate of change of the mass contained in that volume is equal to the rate at which mass is passing through the surface from outside to inside, minus the rate at which mass is passing from inside to outside. This can be expressed as an equation in integral form over the control volume.
The continuum assumption is an idealization of continuum mechanics under which fluids can be treated as continuous, even though, on a microscopic scale, they are composed of molecules. Under the continuum assumption, macroscopic (observed/measurable) properties such as density, pressure, temperature, and bulk velocity are taken to be well-defined at "infinitesimal" volume elements—small in comparison to the characteristic length scale of the system, but large in comparison to molecular length scale
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...Infopitaara
A Boiler Feed Pump (BFP) is a critical component in thermal power plants. It supplies high-pressure water (feedwater) to the boiler, ensuring continuous steam generation.
⚙️ How a Boiler Feed Pump Works
Water Collection:
Feedwater is collected from the deaerator or feedwater tank.
Pressurization:
The pump increases water pressure using multiple impellers/stages in centrifugal types.
Discharge to Boiler:
Pressurized water is then supplied to the boiler drum or economizer section, depending on design.
🌀 Types of Boiler Feed Pumps
Centrifugal Pumps (most common):
Multistage for higher pressure.
Used in large thermal power stations.
Positive Displacement Pumps (less common):
For smaller or specific applications.
Precise flow control but less efficient for large volumes.
🛠️ Key Operations and Controls
Recirculation Line: Protects the pump from overheating at low flow.
Throttle Valve: Regulates flow based on boiler demand.
Control System: Often automated via DCS/PLC for variable load conditions.
Sealing & Cooling Systems: Prevent leakage and maintain pump health.
⚠️ Common BFP Issues
Cavitation due to low NPSH (Net Positive Suction Head).
Seal or bearing failure.
Overheating from improper flow or recirculation.
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...Infopitaara
Classes and Objects
1. Lecture By,
Prabu.U
Assistant Professor,
Department of Computer Science and Engineering.
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING,
V R Siddhartha Engineering College.
20ES3102
JAVA PROGRAMMING
UNIT 1
2. UNIT I:
Introduction, The History and Evolution of Java: Java history and evolution,
Java features, Java’s Magic: Byte Code, How java differs from C and C++.
An Overview of Java: Object Oriented Programming: Two paradigms,
Principles of OOP, A First simple Program and Control statements.
Data Types, Variables and Arrays: Java keywords, Primitive types, Integers,
Floating-Point Types, Characters, Booleans, Variables, Operators, Type
Conversion, Casting and Arrays.
Introducing Classes and objects: Class fundamentals, declaring objects,
assigning object reference variables, introducing methods, constructors, this
keyword, Garbage collection, overloading methods, using objects as
parameters, returning objects, static and final keywords, nested and inner
classes.
20ES3102 – JAVA PROGRAMMING
3. 1. Java History and Evolution
2. Java Features
3. Java’s Magic: Byte Code
4. How java differs from C and C++
INTRODUCTION, THE HISTORY AND EVOLUTION OF JAVA
4. Java’s Lineage
Java is related to C++, which is a direct descendant of C.
Much of the characteristics of Java is inherited from these two
languages.
From C, Java derives its syntax. Many of Java’s object-oriented
features were influenced by C++.
In fact, several of Java’s defining characteristics come from—or are
responses to—its predecessors.
5. What is Java?
Java is a high-level programming language developed by Sun
Microsystems.
It was originally designed for developing programs for set-top
boxes and handheld devices, but later became a popular choice for
creating web applications.
The Java syntax is similar to C++, but is strictly an object-oriented
programming language.
For example, most Java programs contain classes, which are used
to define objects, and methods, which are assigned to individual
classes.
6. Java is also known for being more strict than C++, meaning variables
and functions must be explicitly defined.
This means Java source code may produce errors or "exceptions" more
easily than other languages, but it also limits other types of errors that
may be caused by undefined variables or unassigned types.
Unlike Windows executables (.EXE files) or Macintosh applications
(.APP files), Java programs are not run directly by the operating system.
Instead, Java programs are interpreted by the Java Virtual Machine, or
JVM, which runs on multiple platforms.
This means all Java programs are multiplatform and can run on
different platforms, including Macintosh, Windows, and Unix
computers. However, the JVM must be installed for Java applications or
applets to run at all.
7. 1. Java History and Evolution
Java was conceived by James Gosling, Patrick Naughton, Chris Warth,
Ed Frank, and Mike Sheridan at Sun Microsystems, Inc. in 1991.
It took 18 months to develop the first working version. This language
was initially called “Oak,” but was renamed “Java” in 1995.
Between the initial implementation of Oak in the fall of 1992 and the
public announcement of Java in the spring of 1995, many more people
contributed to the design and evolution of the language.
Bill Joy, Arthur van Hoff, Jonathan Payne, Frank Yellin, and Tim
Lindholm were key contributors to the maturing of the original
prototype.
8. The language was initially called Oak after an oak tree that stood
outside Gosling's office.
Later the project went by the name Green and was finally renamed
Java, from Java coffee, the coffee from Indonesia in 1995.
It promised Write Once, Run Anywhere (WORA) functionality,
providing no-cost run-times on popular platforms.
The three editions of Java are
Java Mobile / Micro Edition (ME)
Java Standard Edition (SE)
Java Enterprise Edition (EE)
9. The Evolution of Java
Java continued to evolve at an explosive pace.
Soon after the release of Java 1.0, the designers of Java had already
created Java 1.1.
Java 1.1 added many new library elements, redefined the way
events are handled, and reconfigured many features of the 1.0
library.
The next major release of Java was Java 2, where the “2” indicates
“second generation.”
10. The first release of Java 2 carried the version number 1.2. It may
seem odd that the first release of Java 2 used the 1.2 version
number.
The reason is that it originally referred to the internal version
number of the Java libraries.
Java 2 added support for a number of new features, such as Swing
and the Collections Framework, and it enhanced the Java Virtual
Machine and various programming tools.
Java 2 also contained a few deprecations. The most important
affected the Thread class in which the methods suspend( ),
resume( ), and stop( ) were deprecated.
11. J2SE 1.3 was the first major upgrade to the original Java 2 release.
For the most part, it added to existing functionality and “tightened
up” the development environment.
The release of J2SE 1.4 further enhanced Java. This release
contained several important upgrades, enhancements, and
additions.
For example, it added the new keyword assert, chained exceptions,
and a channel-based I/O subsystem.
It also made changes to the Collections Framework and the
networking classes. In addition, numerous small changes were
made throughout.
12. The next release of Java was J2SE 5, and it was revolutionary.
To grasp the magnitude of the changes that J2SE 5 made to Java,
consider the following list of its major new features:
Generics
Annotations
Autoboxing and auto-unboxing
Enumerations
Enhanced, for-each style for loop
Variable-length arguments (varargs)
Static import
Formatted I/O
Concurrency utilities
13. The importance of these new features is reflected in the use of the
version number “5.”
The next version number for Java would normally have been 1.5.
However, the new features were so significant that a shift from 1.4
to 1.5 just didn’t seem to express the magnitude of the change.
Instead, Sun elected to increase the version number to 5 as a way of
emphasizing that a major event was taking place. Thus, it was
named J2SE 5, and the developer’s kit was called JDK 5.
However, in order to maintain consistency, Sun decided to use 1.5
as its internal version number, which is also referred to as the
developer version number. The “5” in J2SE 5 is called the product
version number.
14. The next release of Java was called Java SE 6. Sun once again
decided to change the name of the Java platform.
First, notice that the “2” was dropped. Thus, the platform was now
named Java SE, and the official product name was Java Platform,
Standard Edition 6.
The Java Development Kit was called JDK 6. As with J2SE 5, the 6
in Java SE 6 is the product version number. The internal, developer
version number is 1.6.
Java SE 6 built on the base of J2SE 5, adding incremental
improvements. Java SE 6 added no major features to the Java
language proper, but it did enhance the API libraries, added
several new packages, and offered improvements to the runtime.
15. Java SE 7 was the next release of Java, with the Java Development
Kit being called JDK 7, and an internal version number of 1.7.
Java SE 7 was the first major release of Java since Sun
Microsystems was acquired by Oracle.
Java SE 7 contained many new features, including significant
additions to the language and the API libraries.
Upgrades to the Java run-time system that support non-Java
languages were also included, but it is the language and library
additions that were of most interest to Java programmers.
16. The new language features were developed as part of Project Coin.
The purpose of Project Coin was to identify a number of small
changes to the Java language that would be incorporated into JDK
7.
Although these features were collectively referred to as “small,”
the effects of these changes have been quite large in terms of the
code they impact.
Here is a list of the language features added by JDK 7:
A String can now control a switch statement.
Binary integer literals
Underscores in numeric literals
17. An expanded try statement, called try-with-resources, that
supports automatic resource management. (For example,
streams can be closed automatically when they are no longer
needed.)
Type inference (via the diamond operator) when constructing a
generic instance.
Enhanced exception handling in which two or more exceptions
can be caught by a single catch (multi-catch) and better type
checking for exceptions that are rethrown.
Although not a syntax change, the compiler warnings associated
with some types of varargs methods were improved, and you
have more control over the warnings.
18. Java SE 7 made several additions to the Java API library.
Two of the most important were the enhancements to the NIO
Framework and the addition of the Fork/Join Framework.
NIO (which originally stood for New I/O) was added to Java in
version 1.4.
However, the changes added by Java SE 7 fundamentally expanded
its capabilities.
So significant were the changes, that the term NIO.2 is often used.
19. The Fork/Join Framework provides important support for parallel
programming. Parallel programming is the name commonly given
to the techniques that make effective use of computers that contain
more than one processor, including multicore systems.
The advantage that multicore environments offer is the prospect of
significantly increased program performance. The Fork/Join
Framework addressed parallel programming by
Simplifying the creation and use of tasks that can execute
concurrently
Automatically making use of multiple processors
Therefore, by using the Fork/Join Framework, you can easily create
scalable applications that automatically take advantage of the
processors available in the execution environment.
20. The newest release of Java is Java SE 8, with the developer’s kit being
called JDK 8.
It has an internal version number of 1.8. JDK 8 represents a very
significant upgrade to the Java language because of the inclusion of a
far-reaching new language feature: the lambda expression.
The inclusion of lambda expressions has also had a wide-ranging effect
on the Java libraries, with new features being added to take advantage of
them.
One of the most important is the new stream API, which is packaged in
java.util.stream. The stream API supports pipeline operations on data
and is optimized for lambda expressions.
Another very important new package is java.util.function. It defines a
number of functional interfaces.
22. (i) Object Oriented
In Java, everything is an Object. Java can be easily extended since it
is based on the Object model.
(ii) Simple
Java is very easy to learn, and its syntax is simple, clean and easy to
understand.
According to Sun, Java language is a simple programming
language because:
Java syntax is based on C and influenced by C++.
Java has removed many complicated and rarely-used features,
for example, explicit pointers, operator overloading, etc.
There is no need to remove unreferenced objects because there is
an Automatic Garbage Collection in Java.
23. (iii) Secured
Java is best known for its security. With Java, we can develop virus-
free systems.
24. Java is secured because:
No explicit pointer
Java Programs run inside a virtual machine sandbox
Classloader: Classloader in Java is a part of the Java Runtime
Environment(JRE) which is used to load Java classes into the Java
Virtual Machine dynamically. It adds security by separating the package
for the classes of the local file system from those that are imported from
network sources.
Bytecode Verifier: It checks the code fragments for illegal code that can
violate access right to objects.
Security Manager: It determines what resources a class can access such
as reading and writing to the local disk.
25. (iv) Platform Independent
Unlike many other programming languages including C and C++,
when Java is compiled, it is not compiled into platform specific
machine, rather into platform independent byte code.
26. (v) Robust
Robust simply means strong. Java is robust because:
It uses strong memory management.
There is a lack of pointers that avoids security problems.
There is automatic garbage collection in java which runs on the
Java Virtual Machine to get rid of objects which are not being
used by a Java application anymore.
There are exception handling and the type checking mechanism
in Java. All these points make Java robust.
27. (vi) Portable
Java is portable because it facilitates you to carry the Java bytecode to
any platform.
(vii) Architectural Neutral
Compiler generates bytecodes, which have nothing to do with a
particular computer architecture, hence a Java program is easy to
interpret on any machine.
Java is architecture neutral because there are no implementation
dependent features, for example, the size of primitive types is fixed.
In C programming, int data type occupies 2 bytes of memory for 32-bit
architecture and 4 bytes of memory for 64-bit architecture. However, it
occupies 4 bytes of memory for both 32 and 64-bit architectures in Java.
28. (viii) Dynamic
Java is considered to be more dynamic than C or C++ since it is
designed to adapt to an evolving environment.
Java programs can carry extensive amount of run-time information
that can be used to verify and resolve accesses to objects on run-
time.
(ix) Interpreted
Java programs are compiled into an intermediate byte code format,
which in turn will be interpreted by the VM at run time.
Hence, any Java program is checked twice before it actually runs.
29. (x) High Performance
With the use of Just-In-Time compilers, Java enables high performance.
(xi) Multi-threaded
With Java's multithreaded feature it is possible to write programs that
can perform many tasks simultaneously. This design feature allows the
developers to construct interactive applications that can run smoothly.
(xii) Distributed
Java is distributed because it facilitates users to create distributed
applications in Java.
RMI and EJB are used for creating distributed applications. This feature
of Java makes us able to access files by calling the methods from any
machine on the internet.
30. 3. Java’s Magic: Byte Code
Java is a high level programming language. A program written in high
level language cannot be run on any machine directly.
First, it needs to be translated into that particular machine language.
The javac compiler does this thing, it takes java program (.java file
containing source code) and translates it into machine code (referred as
byte code or .class file).
A Java class file is a file (with the .class filename extension) containing
Java bytecode that can be executed on the Java Virtual Machine (JVM).
Java Virtual Machine (JVM) is a virtual machine that resides in the real
machine (your computer) and the machine language for JVM is byte
code.
31. This makes it easier for compiler as it has to generate byte code for
JVM rather than different machine code for each type of machine.
JVM executes the byte code generated by compiler and produce
output. JVM is the one that makes java platform independent.
So, now we understood that the primary function of JVM is to
execute the byte code produced by compiler.
Each operating system has different JVM, however the output they
produce after execution of byte code is same across all operating
systems which means that the byte code generated on Windows
can be run on Mac OS and vice versa.
32. Working of JVM
First, Java code is complied into bytecode. This bytecode gets
interpreted on different machines
Between host system and Java source, Bytecode is an intermediary
language.
JVM is responsible for allocating memory space.
34. 1. Class Loader
The class loader is a subsystem used for loading class files.
It performs three major functions such as Loading, Linking, and Initialization.
Loading: The Class loader reads the .class file, generate the corresponding
binary data and save it in method area.
Linking: Performs verification, preparation, and (optionally) resolution.
Verification: It ensures the correctness of .class file i.e. it check whether
this file is properly formatted and generated by valid compiler or not. If
verification fails, we get run-time exception java.lang.VerifyError.
Preparation: JVM allocates memory for class variables and initializing the
memory to default values.
Resolution: It is the process of replacing symbolic references from the type
with direct references. It is done by searching into method area to locate
the referenced entity.
Initialization: In this phase, all static variables are assigned with their values
defined in the code and static block(if any).
35. 2. Method Area
JVM Method Area stores class structures like metadata, the constant
runtime pool, and the code for methods.
3. Heap
All the Objects, their related instance variables, and arrays are
stored in the heap. This memory is common and shared across
multiple threads.
4. JVM Language Stacks
For every thread, a separate runtime stack will be created. For every
method call, one entry will be made in the stack memory which is
called Stack Frame. All local variables will be created in the stack
memory
36. 5. Program Counter (PC) Registers
Each thread will have separate PC Registers, to hold the address of
current executing instruction once the instruction is executed the PC
register will be updated with the next instruction.
6. Native Method Stacks
Native Method Stack holds native method information. For every
thread, a separate native method stack will be created.
7. Execution Engine
Execution engine execute the .class (bytecode). It reads the byte-
code line by line, use data and information present in various
memory area and execute instructions. It can be classified in three
parts :- Interpreter, Just-In-Time (JIT) Compiler, Garbage Collector.
37. Interpreter
It interprets the bytecode line by line and then executes.
The disadvantage here is that when one method is called multiple
times, every time interpretation is required.
Just-In-Time Compiler(JIT)
It is used to increase efficiency of interpreter.
It compiles the entire bytecode and changes it to native code so
whenever interpreter see repeated method calls, JIT provide direct
native code for that part so re-interpretation is not required, thus
efficiency is improved.
Garbage Collector
Collects and removes unreferenced objects.
38. 8. Native Method Interface
The Native Method Interface is a programming framework.
It allows Java code which is running in a JVM to call by libraries and
native applications.
9. Native Method Libraries
Native Libraries is a collection of the Native Libraries(C, C++) which
are needed by the Execution Engine.
39. Software Code Compilation & Execution Process
In order to write and execute a software program, you need the following
1) Editor – To type your program into, a notepad could be used for this
2) Compiler – To convert your high language program into native machine code
3) Linker – To combine different program files reference in your main program
together.
4) Loader – To load the files from your secondary storage device like Hard Disk,
Flash Drive, CD into RAM for execution. The loading is automatically done when
you execute your code.
5) Execution – Actual execution of the code which is handled by your OS &
processor.
40. Java Code Compilation and Execution in Java VM
In your main, you have two methods f1 and f2.
The main method is stored in file a1.java
f1 is stored in a file as a2.java and f2 is stored in a file as a3.java
41. The compiler will compile the three files and produces 3
corresponding .class file which consists of BYTE code.
The Java VM or Java Virtual Machine resides on the RAM. During
execution, using the class loader the class files are brought on the
RAM. The BYTE code is verified for any security breaches.
42. Next, the execution engine will convert the Bytecode into Native
machine code. This is just in time compiling.
43. 4. How Java differs from C and C++
Metrics C C++ Java
Programming
Paradigm
Procedural language Object-Oriented
Programming (OOP)
Pure Object Oriented
Oriented
Developer Dennis Ritchie in 1972 Bjarne Stroustrup in 1979 James Gosling in 1991
Translator Compiler only Compiler only Interpreted language
(Compiler +
interpreter)
File generation .exe files .exe files .class files
Pre-processor
directives
Supports header files
(#include, #define)
Supports header files
(#include, #define)
Uses Packages (import)
Allocation Uses malloc, calloc Use new, delete Garbage collector
Keywords 32 63 50 defined keywords
Approach Top-down approach Top-down approach Bottom-up approach
44. 1. Object Oriented Programming
Two paradigms
The Three OOP Principles
2. A First simple Program
3. Control statements
AN OVERVIEW OF JAVA
45. 1. Object Oriented Programming
Object-oriented programming (OOP) is a programming methodology
that helps organize complex programs through the use of inheritance,
encapsulation, and polymorphism.
OOP is an approach to program organization and development, which
attempts to eliminate some of the pitfalls of conventional programming
methods by incorporating the best of structured programming features
with several new concepts.
It is a new way of organizing and developing programs and has nothing
to do with any particular language.
C++ is basically a procedural language with Object-oriented extension
whereas Java is a pure object-oriented language.
46. Two Paradigm
All computer programs consist of two elements: code and data.
Furthermore, a program can be conceptually organized around its
code or around its data.
That is, some programs are written around “what is happening”
and others are written around “who is being affected.”
These are the two paradigms that govern how a program is
constructed.
47. The first way is called the process-oriented model.
This approach characterizes a program as a series of linear steps
(that is, code).
The process-oriented model can be thought of as code acting on
data.
Procedural languages such as C employ this model to considerable
success.
48. To manage increasing complexity, the second approach, called
object-oriented programming, was conceived.
Object-oriented programming organizes a program around its data
(that is, objects) and a set of well-defined interfaces to that data.
An object-oriented program can be characterized as data
controlling access to code.
49. The Three OOP Principles
All object-oriented programming languages provide mechanisms that
help you implement the object-oriented model.
They are encapsulation, inheritance, and polymorphism.
The General concepts of OOP which form the heart of Java language are
as follows:
Objects
Classes
Data Abstraction
Encapsulation
Inheritance
Polymorphism
Dynamic Binding
Message Communication
50. Objects
Objects are basic runtime entities in an object-oriented system.
An object is an instance of a class.
The object is an entity which has state and behavior.
An object has three characteristics:
State: State represents properties of an object. It is represented by
instance variable/attribute of an object. The properties of an
object are important because the outcome of functions depend on
the properties.
Behavior: Behavior represents functionality or actions. It is
represented by methods in Java.
Identity: Identity represents the unique name of an object. It
differentiates one object from the other. The unique name of an
object is used to identify the object.
51. Classes
The entire set of data and code of an object can be made a user-
defined data type using the concepts of a class.
A class may be thought of as a ‘datatype’ and an object as a
‘variable’ of that data type.
Once a class has been defined, we can create any number of objects
belonging to that class.
Each object is associated with the data of type class with which
they are created.
A class is the blueprint from which individual objects are created.
53. Data Abstraction
Abstraction refers to the act of representing essential features without
including the background details or explanations.
Classes use the concept of abstraction and are defined as a list of
abstract attributes such as size, weight and cost, and methods that
operate on these attributes.
Encapsulation
The wrapping up of data and methods into a single unit (called class) is
known as encapsulation. It is the most striking feature of a class.
The data is not accessible to the outside world and only those methods,
which are wrapped in the class, can access it.
These method provide the interface between the object’s data and the
program.
54. Inheritance
Inheritance is the process by which objects of one class acquire the
properties of objects of another class.
Inheritance supports the concept of hierarchical classification.
For example, the bird robin is the part of the class flying bird,
which is again a part of the class bird.
55. In OOP, the concept of inheritance provides the idea of reusability.
This means that we can add additional features to an existing class
without modifying it.
This is possible by deriving a new class from the existing one.
The new class will have the combined features of both the classes.
Thus the real appeal and power of the inheritance mechanism is
that it allows the programmer to reuse a class that is almost, but not
exactly, what he wants and to tailor the class in such a way that it
does not introduce any undesirable side effects into the rest of the
classes.
In Java, the derived class is known as ‘subclass’.
Without inheritance, each class would have to explicitly include all
of its features.
56. Polymorphism
Polymorphism means the ability to take more than one form.
For example, an operation may exhibit different behaviour in
different instances.
The behaviour depends upon the types of data used in the
operation.
57. Dynamic Binding
Dynamic binding means that the code associated with a given
procedure call in not known until the time of the call at runtime.
It is associated with polymorphism and inheritance.
A procedure call associated with a polymorphic reference depends
on the dynamic type of that reference.
Consider the procedure “area”. By inheritance, every object will
have this procedure.
Its algorithm is, however, unique to each object and so the area
procedure will be redefined in each class that defines the object.
At run-time, the code matching the object under current reference
will be called.
58. Message Communication
An object-oriented program consists of a set of objects that
communicate with each other.
The process of programming in an object-oriented language,
therefore, involves the following basic steps:
Create classes that define objects and their behaviour
Create objects from class definitions
Establish communication among objects through message
passing
59. Objects communicate with other by sending and receiving
information much the same way as people pass messages to one
another.
60. A message for an object is a request for execution of a procedure,
and therefore will invoke a method (procedure) in the receiving
object that generates the desired result.
Message passing involves specifying the name of the object, the
name of the method(message) and the information to be sent. For
example, consider the statement
emp.salary(name);
Here, emp is the object, salary is the message and the name is the
parameter that contains information.
Objects have a life cycle. They can be created and destroyed.
Communication with an object is feasible as long as it is alive.
61. 2. A First Simple Program
class Example
{
// Your program begins with a call to main().
public static void main(String args[])
{
System.out.println("This is a simple Java program.");
}
}
C:>javac Example.java
C:>java Example
62. The public keyword is an access modifier, which allows the programmer
to control the visibility of class members.
When a class member is preceded by public, then that member may be
accessed by code outside the class in which it is declared. (The opposite
of public is private, which prevents a member from being used by code
defined outside of its class.)
In this case, main( ) must be declared as public, since it must be called by
code outside of its class when the program is started.
The keyword static allows main( ) to be called without having to
instantiate a particular instance of the class. This is necessary since
main( ) is called by the Java Virtual Machine before any objects are
made.
The keyword void simply tells the compiler that main( ) does not return
a value.
63. Any information that you need to pass to a method is received by
variables specified within the set of parentheses that follow the
name of the method.
These variables are called parameters. If there are no parameters
required for a given method, you still need to include the empty
parentheses.
In main( ), there is only one parameter, albeit a complicated one.
String args[ ] declares a parameter named args, which is an array of
instances of the class String. (Arrays are collections of similar
objects.)
Objects of type String store character strings. In this case, args
receives any command-line arguments present when the program
is executed.
64. 1. Java Keywords
2. Primitive Types
3. Integers, Floating-Point Types, Characters, Booleans
4. Variables
5. Type Conversion and Casting
6. Arrays
DATA TYPES, VARIABLES AND ARRAYS
65. 1. Java Keywords
There are 50 keywords currently defined in the Java language.
These keywords, combined with the syntax of the operators and
separators, form the foundation of the Java language.
These keywords cannot be used as identifiers. Thus, they cannot be
used as names for a variable, class, or method.
The keywords const and goto are reserved but not used. In the
early days of Java, several other keywords were reserved for
possible future use.
In addition to the keywords, Java reserves the following: true, false,
and null. These are values defined by Java.
You may not use these words for the names of variables, classes,
and so on.
67. 2. Primitive Types
Java defines eight primitive types of data: byte, short, int, long, char,
float, double, and boolean.
The primitive types are also commonly referred to as simple types.
Integers: This group includes byte, short, int, and long, which are for
whole-valued signed numbers.
Floating-point numbers: This group includes float and double, which
represent numbers with fractional precision.
Characters: This group includes char, which represents symbols in a
character set, like letters and numbers.
Boolean: This group includes boolean, which is a special type for
representing true/false values.
68. 3. Integers, Floating-Point Types, Characters, Booleans
Integers:
Java defines four integer types: byte, short, int, and long. All of
these are signed, positive and negative values.
Java does not support unsigned, positive-only integers. Many other
computer languages support both signed and unsigned integers.
However, Java’s designers felt that unsigned integers were
unnecessary.
Specifically, they felt that the concept of unsigned was used mostly
to specify the behavior of the high-order bit, which defines the sign
of an integer value.
69. byte
The smallest integer type is byte. This is a signed 8-bit type that has
a range from –128 to 127.
Variables of type byte are especially useful when you’re working
with a stream of data from a network or file.
They are also useful when you’re working with raw binary data
that may not be directly compatible with Java’s other built-in types.
byte b, c;
70. short
short is a signed 16-bit type. It has a range from –32,768 to 32,767. It is
probably the least used Java type.
Here are some examples of short variable declarations:
short s;
int
The most commonly used integer type is int. It is a signed 32-bit type.
In addition to other uses, variables of type int are commonly employed
to control loops and to index arrays.
long
long is a signed 64-bit type and is useful for those occasions where an int
type is not large enough to hold the desired value.
The range of a long is quite large. This makes it useful when big, whole
numbers are needed.
71. Floating-Point Types
Floating-point numbers, also known as real numbers, are used
when evaluating expressions that require fractional precision.
For example, calculations such as square root, or transcendentals
such as sine and cosine, result in a value whose precision requires a
floating point type.
Java implements the standard (IEEE–754) set of floating-point types
and operators.
There are two kinds of floating-point types, float and double,
which represent single- and double-precision numbers,
respectively.
72. float
The type float specifies a single-precision value that uses 32 bits of
storage.
Single precision is faster on some processors and takes half as much
space as double precision, but will become imprecise when the values
are either very large or very small.
Variables of type float are useful when you need a fractional component,
but don’t require a large degree of precision.
For example, float can be useful when representing dollars and cents.
Here are some example float variable declarations:
float hightemp, lowtemp;
73. Double
Double precision, as denoted by the double keyword, uses 64 bits
to store a value.
Double precision is actually faster than single precision on some
modern processors that have been optimized for high-speed
mathematical calculations.
All transcendental math functions, such as sin( ), cos( ), and sqrt( ),
return double values.
When you need to maintain accuracy over many iterative
calculations, or are manipulating large-valued numbers, double is
the best choice.
74. A short program that uses double variables to compute the area of a circle
class Area
{
public static void main(String args[])
{
double pi, r, a;
r = 10.8; // radius of circle
pi = 3.1416; // pi, approximately
a = pi * r * r; // compute area
System.out.println("Area of circle is " + a);
}
}
75. Characters
In Java, the data type used to store characters is char.
However, C/C++ programmers beware: char in Java is not the same as
char in C or C++. In C/C++, char is 8 bits wide. This is not the case in
Java.
Instead, Java uses Unicode to represent characters. Unicode defines a
fully international character set that can represent all of the characters
found in all human languages.
It is a unification of dozens of character sets, such as Latin, Greek,
Arabic, Cyrillic, Hebrew, Katakana, Hangul, and many more.
At the time of Java's creation, Unicode required 16 bits. Thus, in Java
char is a 16-bit type.
76. The range of a char is 0 to 65,536.
There are no negative chars.
The standard set of characters known as ASCII still ranges from 0
to 127 as always, and the extended 8-bit character set, ISO-Latin-1,
ranges from 0 to 255.
Since Java is designed to allow programs to be written for
worldwide use, it makes sense that it would use Unicode to
represent characters.
77. import java.io.*;
class CharDemo
{
public static void main(String args[])
{
char ch1, ch2;
ch1 = 88; // code for X
ch2 = 'Y';
System.out.print("ch1 and ch2: ");
System.out.print(ch1 + " " + ch2);
}
}
ch1 and ch2: X Y
78. Notice that ch1 is assigned the value 88, which is the ASCII (and
Unicode) value that corresponds to the letter X.
As mentioned, the ASCII character set occupies the first 127 values
in the Unicode character set.
Although char is designed to hold Unicode characters, it can also
be used as an integer type on which you can perform arithmetic
operations.
For example, you can add two characters together, or increment the
value of a character variable.
79. import java.io.*;
class CharDemo2
{
public static void main(String args[])
{
char ch1;
ch1 = 'X';
System.out.println("ch1 contains " + ch1);
ch1++; // increment ch1
System.out.println("ch1 is now " + ch1);
}
}
ch1 contains X
ch1 is now Y
80. In the program, ch1 is first given the value X. Next, ch1 is
incremented. This results in ch1
containing Y, the next character in the ASCII (and Unicode)
sequence.
81. Booleans
Java has a primitive type, called boolean, for logical values. It can
have only one of two possible values, true or false.
This is the type returned by all relational operators, as in the case of
a < b.
boolean is also the type required by the conditional expressions
that govern the control statements such as if and for.
82. import java.io.*;
class BoolTest
{
public static void main(String args[])
{
boolean b;
b = false;
System.out.println("b is " + b);
b = true;
System.out.println("b is " + b);
// a boolean value can control the if statement
if(b)
System.out.println("This is executed.");
b = false;
if(b)
System.out.println("This is not executed.");
//outcome of a relational operator is a boolean value
System.out.println("10 > 9 is " + (10 > 9));
}
}
83. b is false
b is true
This is executed.
10 > 9 is true
There are three interesting things to notice about this program. First, as you can
see, when a boolean value is output by println( ), "true" or "false" is displayed.
Second, the value of a boolean variable is sufficient, by itself, to control the if
statement. There is no need to write an if statement like this:
if(b == true) …
Third, the outcome of a relational operator, such as <, is a boolean value. This
is why the expression 10>9 displays the value "true."
Further, the extra set of parentheses around 10>9 is necessary because the +
operator has a higher precedence than the >.
84. 4. Variables
The variable is the basic unit of storage in a Java program.
A variable is defined by the combination of an identifier, a type,
and an optional initializer.
In addition, all variables have a scope, which defines their
visibility, and a lifetime.
85. Declaring a Variable
In Java, all variables must be declared before they can be used. The
basic form of a variable declaration is
type identifier; type identifier= value;
The identifier is the name of the variable. You can initialize the
variable by specifying an equal sign and a value.
Keep in mind that the initialization expression must result in a
value of the same (or compatible) type as that specified for the
variable.
86. int a, b, c; // declares three ints, a, b, and c.
int d = 3, e, f = 5; // declares three more ints, initializing d and f.
byte z = 22; // initializes z.
double pi = 3.14159; // declares an approximation of pi.
char x = 'x'; // the variable x has the value 'x'.
87. Dynamic Initialization
Java allows variables to be initialized dynamically, using any
expression valid at the time the variable is declared.
class DynInit
{
public static void main(String args[])
{
double a = 3.0, b = 4.0;
// c is dynamically initialized
double c = Math.sqrt(a * a + b * b);
System.out.println("Hypotenuse is " + c);
}
}
88. Here, three local variables—a, b, and c—are declared.
The first two, a and b, are initialized by constants.
However, c is initialized dynamically to the length of the
hypotenuse (using the Pythagorean theorem).
The program uses another of Java’s built-in methods, sqrt( ), which
is a member of the Math class, to compute the square root of its
argument.
89. The Scope and Lifetime of Variables
So far, all of the variables used have been declared at the start of
the main( ) method.
However, Java allows variables to be declared within any block.
A block is begun with an opening curly brace and ended by a
closing curly brace. A block defines a scope.
Thus, each time you start a new block, you are creating a new
scope.
A scope determines what objects are visible to other parts of
your program. It also determines the lifetime of those objects.
90. class Scope
{
public static void main(String args[])
{
int x; // known to all code within main
x = 10;
if(x == 10)
{
// start new scope
int y = 20; // known only to this block
// x and y both known here.
System.out.println("x and y: " + x + " " + y);
x = y * 2;
}
// y = 100; // Error! y not known here
// x is still known here
System.out.println("x is " + x);
}
}
91. Variables are created when their scope is entered, and
destroyed when their scope is left.
This means that a variable will not hold its value once it has
gone out of scope. Therefore, variables declared within a
method will not hold their values between calls to that
method.
Also, a variable declared within a block will lose its value
when the block is left. Thus, the lifetime of a variable is
confined to its scope.
If a variable declaration includes an initializer, then that
variable will be reinitialized each time the block in which it is
declared is entered.
92. class LifeTime
{
public static void main(String args[])
{
int x;
for(x = 0; x < 3; x++)
{
int y = -1; // y is initialized each time block is entered
System.out.println("y is: " + y); // this always prints -1
y = 100;
System.out.println("y is now: " + y);
}
}
}
93. y is: -1
y is now: 100
y is: -1
y is now: 100
y is: -1
y is now: 100
y is reinitialized to –1 each time the inner for loop is entered.
Even though it is subsequently assigned the value 100, this value is lost.
94. Although blocks can be nested, you cannot declare a variable to have the same
name as one in an outer scope.
class ScopeErr
{
public static void main(String args[])
{
int bar = 1;
{
// creates a new scope
int bar = 2; // Compile-time error – bar already defined!
}
}
}
95. 5. Type Conversion and Casting
If the two types are compatible, then Java will perform the
conversion automatically. For example, it is always possible to
assign an int value to a long variable.
However, not all types are compatible, and thus, not all type
conversions are implicitly allowed.
For instance, there is no automatic conversion defined from double
to byte.
Fortunately, it is still possible to obtain a conversion between
incompatible types. To do so, you must use a cast, which performs
an explicit conversion between incompatible types.
96. Java’s Automatic Conversions
When one type of data is assigned to another type of variable, an
automatic type conversion will take place if the following two
conditions are met:
The two types are compatible.
The destination type is larger than the source type.
When these two conditions are met, a widening conversion takes
place. For example, the int type is always large enough to hold all
valid byte values, so no explicit cast statement is required.
97. For widening conversions, the numeric types, including integer and
floating-point types, are compatible with each other.
However, there are no automatic conversions from the numeric
types to char or boolean.
Also, char and boolean are not compatible with each other.
98. Casting Incompatible Types
Although the automatic type conversions are helpful, they will not
fulfil all needs.
For example, what if you want to assign an int value to a byte
variable?
This conversion will not be performed automatically, because a
byte is smaller than an int.
This kind of conversion is sometimes called a narrowing
conversion, since you are explicitly making the value narrower so
that it will fit into the target type.
99. To create a conversion between two incompatible types, you
must use a cast.
A cast is simply an explicit type conversion. It has this general
form:
(target-type) value
Here, target-type specifies the desired type to convert the
specified value to. For example, the following fragment casts
an int to a byte.
If the integer’s value is larger than the range of a byte, it will
be reduced modulo (the remainder of an integer division by
the) byte’s range.
100. int a;
byte b;
// …
b = (byte) a;
A different type of conversion will occur when a floating-point value is
assigned to an integer type: truncation. As you know, integers do not
have fractional components.
Thus, when a floating-point value is assigned to an integer type, the
fractional component is lost.
For example, if the value 1.23 is assigned to an integer, the resulting
value will simply be 1.
The 0.23 will have been truncated. Of course, if the size of the whole
number component is too large to fit into the target integer type, then
that value will be reduced modulo the target type’s range.
101. class Conversion
{
public static void main(String args[])
{
byte b;
int i = 257;
double d = 323.142;
System.out.println("nConversion of int to byte.");
b = (byte) i;
System.out.println("i and b " + i + " " + b);
System.out.println("nConversion of double to int.");
i = (int) d;
System.out.println("d and i " + d + " " + i);
System.out.println("nConversion of double to byte.");
b = (byte) d;
System.out.println("d and b " + d + " " + b);
}
}
102. Conversion of int to byte.
i and b 257 1
Conversion of double to int.
d and i 323.142 323
Conversion of double to byte.
d and b 323.142 67
103. Let’s look at each conversion. When the value 257 is cast into a
byte variable, the result is the remainder of the division of 257 by
256 (the range of a byte), which is 1 in this case.
When the d is converted to an int, its fractional component is lost.
When d is converted to a byte, its fractional component is lost,
and the value is reduced modulo 256, which in this case is 67.
104. Automatic Type Promotion in Expressions
In addition to assignments, there is another place where certain type
conversions may occur: in expressions.
byte a = 40;
byte b = 50;
byte c = 100;
int d = a * b / c;
The result of the intermediate term a * b easily exceeds the range of
either of its byte operands.
To handle this kind of problem, Java automatically promotes each
byte, short, or char operand to int when evaluating an expression.
This means that the subexpression a*b is performed using integers—
not bytes. Thus, 2,000, the result of the intermediate expression, 50 *
40, is legal even though a and b are both specified as type byte.
105. As useful as the automatic promotions are, they can cause confusing
compile-time errors. For example, this seemingly correct code causes a
problem:
byte b = 50;
b = b * 2; // Error! Cannot assign an int to a byte!
The code is attempting to store 50 * 2, a perfectly valid byte value, back
into a byte variable.
However, because the operands were automatically promoted to int
when the expression was evaluated, the result has also been promoted
to int.
Thus, the result of the expression is now of type int, which cannot be
assigned to a byte without the use of a cast.
This is true even if, as in this particular case, the value being assigned
would still fit in the target type.
106. In cases where you understand the consequences of overflow,
you should use an explicit cast, such as
byte b = 50;
b = (byte)(b * 2);
which yields the correct value of 100.
107. The Type Promotion Rules
Java defines several type promotion rules that apply to
expressions. They are as follows: First, all byte, short, and char
values are promoted to int, as just described.
Then, if one operand is a long, the whole expression is promoted
to long.
If one operand is a float, the entire expression is promoted to
float.
If any of the operands are double, the result is double.
108. class Promote
{
public static void main(String args[])
{
byte b = 42;
char c = 'a’;
short s = 1024;
int i = 50000;
float f = 5.67f;
double d = .1234;
double result = (f * b) + (i / c) - (d * s);
System.out.println((f * b) + " + " + (i / c) + " - " + (d * s));
System.out.println("result = " + result);
}
}
109. double result = (f * b) + (i / c) - (d * s);
In the first subexpression, f * b, b is promoted to a float and the result of
the subexpression is float.
Next, in the subexpression i/c, c is promoted to int, and the result is of
type int.
Then, in d * s, the value of s is promoted to double, and the type of the
subexpression is double.
Finally, these three intermediate values, float, int, and double, are
considered.
The outcome of float plus an int is a float.
Then the resultant float minus the last double is promoted to double,
which is the type for the final result of the expression.
110. 6. Arrays
An array is a group of like-typed variables that are referred to by a
common name.
Arrays of any type can be created and may have one or more
dimensions.
A specific element in an array is accessed by its index.
Arrays offer a convenient means of grouping related information.
111. How to declare an array in Java?
Array can be declared as
dataType[] arrayName;
dataType - it can be primitive data types like int, char, double,
byte, etc. or Java objects.
arrayName - it is an identifier
Example:
double[] data; // declare an array
double[] data = new double[10]; // allocate memory
112. How to initialize arrays in Java?
In the Java array, each memory location is associated with a number.
The number is known as an array index. We can also initialize arrays
in Java, using the index number. For example,
// declare an array
int[] age = new int[3];
// initialize array
age[0] = 10;
age[1] = 20;
age[2] = 30;
Array indices always start from 0. That is, the first element of an array
is at index 0.
If the size of an array is n, then the last element of the array will be at
index n-1.
113. //declare and initialize and array
int[] age = {10, 20, 30, 40, 50};
Here, we have created an array named age and initialized it
with the values inside the curly brackets.
Note that we have not provided the size of the array. In this
case, the Java compiler automatically specifies the size by
counting the number of elements in the array (i.e. 5).
114. How to Access Elements of an Array in Java?
The element of an array can be accessed using the index
number. Here is the syntax for accessing elements of an
array,
// access array elements
array[index]
115. class ArrExample
{
public static void main(String[] args)
{
// create an array
int[] age = {10, 20, 30};
// access each array elements
System.out.println("Accessing Elements of Array:");
System.out.println("First Element: " + age[0]);
System.out.println("Second Element: " + age[1]);
System.out.println("Third Element: " + age[2]);
}
}
Accessing Elements of Array:
First Element: 10
Second Element: 20
Third Element: 30
116. Looping Through Array Elements
class ExampleLoop
{
public static void main(String[] args)
{
int[] age = {10, 20, 30};
System.out.println("Using for Loop:");
for(int i = 0; i < age.length; i++)
{
System.out.println(age[i]);
}
}
}
117. The expression age.length is used in for loop
length property of the array is used to get the size of the
array.
length can be used for int[], double[], String[] to know the
length of the arrays.
length() can be used for String, StringBuilder, etc.
String class related Objects to know the length of the String
118. for-each Loop
Syntax
The syntax of Java for-each loop consists of data_type with the
variable followed by a colon (:), then array or collection.
for(data_type variable : array | collection)
{
//body of for-each loop
}
How it works?
The Java for-each loop traverses the array or collection until
the last element. For each element, it stores the element in the
variable and executes the body of the for-each loop.
119. class ExampleEach
{
public static void main(String[] args)
{
int[] age = {10, 20, 30};
System.out.println("Using for-each Loop:");
for(int a : age)
{
System.out.println(a);
}
}
}
120. One-Dimensional Arrays
A one-dimensional array is, essentially, a list of like-typed variables.
class Average
{
public static void main(String args[])
{
double nums[] = {10.1, 11.2, 12.3, 13.4, 14.5};
double result = 0;
int i;
for(i=0; i<5; i++)
result = result + nums[i];
System.out.println("Average is " + result / 5);
}
}
121. Multidimensional Arrays
In Java, multidimensional arrays are actually arrays of arrays.
To declare a multidimensional array variable, specify each
additional index using another set of square brackets.
For example, the following declares a two dimensional array
variable called twoD:
int twoD[][] = new int[4][5];
122. class TwoDArray
{
public static void main(String args[])
{
int twoD[][]= new int[4][5];
int i, j, k = 0;
for(i=0; i<4; i++)
{
for(j=0; j<5; j++)
{
twoD[i][j] = k;
k++;
}
}
for(i=0; i<4; i++)
{
for(j=0; j<5; j++)
{
System.out.print(twoD[i][j] + " ");
}
System.out.println();
}
}
}
124. 1. Class Fundamentals
2. Declaring Objects
3. Assigning Object Reference Variables
4. Introducing Methods
5. Constructors
6. this Keyword
7. Garbage Collection
CLASSES AND OBJECTS
125. 8. Overloading Methods
9. Using Objects as Parameters
10. Returning Objects
11. Recursion
12. Static and Final Keywords
13. Nested and Inner Classes
CLASSES AND OBJECTS
126. Introducing Classes
The class is at the core of Java.
It is the logical construct upon which the entire Java language is
built because it defines the shape and nature of an object.
As such, the class forms the basis for object-oriented programming
in Java.
Any concept you wish to implement in a Java program must be
encapsulated within a class.
127. 1. Class Fundamentals
Perhaps the most important thing to understand about a class is
that it defines a new data type.
Once defined, this new type can be used to create objects of that
type.
Thus, a class is a template for an object, and an object is an instance
of a class.
128. The General Form of a Class
A class is declared by use of the class keyword.
Classes can (and usually do) get much more complex. A simplified
general form of a class definition is shown in next slide:
129. class classname
{
type instance-variable1;
type instance-variable2;
// ...
type instance-variableN;
type methodname1(parameter-list)
{
// body of method
}
type methodname2(parameter-list)
{
// body of method
}
// ...
type methodnameN(parameter-list)
{
// body of method
}
}
130. The data, or variables, defined within a class are called instance
variables.
The code is contained within methods. Collectively, the methods
and variables defined within a class are called members of the
class.
In most classes, the instance variables are acted upon and accessed
by the methods defined for that class.
Thus, as a general rule, it is the methods that determine how a class
data can be used.
131. Variables defined within a class are called instance variables
because each instance of the class (that is, each object of the class)
contains its own copy of these variables.
Thus, the data for one object is separate and unique from the data
for another.
Java classes do not need to have a main( ) method.
You only specify one if that class is the starting point for your
program.
132. A Simple Class
Here is a class called Box that defines three instance variables:
width, height, and depth.
class Box
{
double width;
double height;
double depth;
}
In this case, the new data type is called Box.
You will use this name to declare objects of type Box. It is
important to remember that a class declaration only creates a
template; it does not create an actual object.
133. To actually create a Box object, you will use a statement like the
following:
Box mybox = new Box(); // create a Box object called mybox
After this statement executes, mybox will be an instance of Box.
As mentioned earlier, each time you create an instance of a class,
you are creating an object that contains its own copy of each
instance variable defined by the class.
Thus, every Box object will contain its own copies of the instance
variables width, height, and depth.
134. To access these variables, you will use the dot (.) operator. The dot
operator links the name of the object with the name of an instance
variable.
For example, to assign the width variable of mybox the value 100, you
would use the following statement:
mybox.width = 100;
This statement tells the compiler to assign the copy of width that is
contained within the mybox object the value of 100.
In general, you use the dot operator to access both the instance variables
and the methods within an object.
Although commonly referred to as the dot operator, the formal
specification for Java categorizes the . as a separator.
135. class Box
{
double width;
double height;
double depth;
}
class BoxDemo
{
public static void main(String args[])
{
Box mybox = new Box();
double vol;
136. mybox.width = 10;
mybox.height = 20;
mybox.depth = 15;
vol = mybox.width * mybox.height * mybox.depth;
System.out.println("Volume is " + vol);
}
}
Volume is 3000.0
You should call the file that contains this program BoxDemo.java,
because the main( ) method is in the class called BoxDemo, not the class
called Box.
When you compile this program, you will find that two .class files have
been created, one for Box and one for BoxDemo.
137. class Box
{
double width;
double height;
double depth;
}
class BoxDemo2
{
public static void main(String args[])
{
Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
138. // assign values to mybox1's instance variables
mybox1.width = 10;
mybox1.height = 20;
mybox1.depth = 15;
// assign different values to mybox2’s instance variables
mybox2.width = 3;
mybox2.height = 6;
mybox2.depth = 9;
// compute volume of first box
vol = mybox1.width * mybox1.height * mybox1.depth;
System.out.println("Volume is " + vol);
// compute volume of second box
vol = mybox2.width * mybox2.height * mybox2.depth;
System.out.println("Volume is " + vol);
}
}
139. Volume is 3000.0
Volume is 162.0
As you can see, mybox1’s data is completely separate from the data
contained in mybox2
140. 2. Declaring Objects
When you create a class, you are creating a new data type. You can
use this type to declare objects of that type.
However, obtaining objects of a class is a two-step process.
First, you must declare a variable of the class type. This variable
does not define an object.
Instead, it is simply a variable that can refer to an object. Second,
you must acquire an actual, physical copy of the object and assign
it to that variable.
141. You can do this using the new operator. The new operator
dynamically allocates (that is, allocates at run time) memory for an
object and returns a reference to it.
This reference is, more or less, the address in memory of the object
allocated by new. This reference is then stored in the variable.
Box mybox; // declare reference to object
mybox = new Box(); // allocate a Box object
142. The first line declares mybox as a reference to an object of type
Box.
At this point, mybox does not yet refer to an actual object.
The next line allocates an object and assigns a reference to it to
mybox.
After the second line executes, you can use mybox as if it were a
Box object.
But in reality, mybox simply holds, in essence, the memory
address of the actual Box object.
144. A Closer Look at new
The new operator dynamically allocates memory for an object. It has
this general form:
class-var = new classname ( );
Here, class-var is a variable of the class type being created. The
classname is the name of the class that is being instantiated.
The class name followed by parentheses specifies the constructor for
the class. A constructor defines what occurs when an object of a class is
created.
Constructors are an important part of all classes and have many
significant attributes. Most real-world classes explicitly define their
own constructors within their class definition.
However, if no explicit constructor is specified, then Java will
automatically supply a default constructor. This is the case with Box.
For now, we will use the default constructor.
145. 3. Assigning Object Reference Variables
Box b1 = new Box();
Box b2 = b1;
After this fragment executes, b1 and b2 will both refer to the same
object.
The assignment of b1 to b2 did not allocate any memory or copy
any part of the original object.
It simply makes b2 refer to the same object as does b1. Thus, any
changes made to the object through b2 will affect the object to
which b1 is referring, since they are the same object.
147. Although b1 and b2 both refer to the same object, they are not
linked in any other way.
For example, a subsequent assignment to b1 will simply unhook b1
from the original object without affecting the object or affecting b2.
For example:
Box b1 = new Box();
Box b2 = b1;
// ...
b1 = null;
Here, b1 has been set to null, but b2 still points to the original object.
148. 4. Introducing Methods
A method is a block of code or collection of statements or a set of
code grouped together to perform a certain task or operation. It is
used to achieve the reusability of code.
This is the general form of a method:
type name(parameter-list)
{
// body of method
}
Here, type specifies the type of data returned by the method. This
can be any valid type, including class types that you create.
149. If the method does not return a value, its return type must be void.
The name of the method is specified by name.
This can be any legal identifier other than those already used by
other items within the current scope.
The parameter-list is a sequence of type and identifier pairs
separated by commas.
Parameters are essentially variables that receive the value of the
arguments passed to the method when it is called.
If the method has no parameters, then the parameter list will be
empty.
150. Methods that have a return type other than void return a value to
the calling routine using the following form of the return statement:
return value;
Here, value is the value returned.
151. Adding a Method to the Box Class
class Box
{
double width;
double height;
double depth;
// display volume of a box
void volume()
{
System.out.print("Volume is ");
System.out.println(width * height * depth);
}
}
152. class BoxDemo3
{
public static void main(String args[])
{
Box mybox1 = new Box();
Box mybox2 = new Box();
// assign values to mybox1's instance variables
mybox1.width = 10;
mybox1.height = 20;
mybox1.depth = 15;
153. //assign different values to mybox2’s instance variables
mybox2.width = 3;
mybox2.height = 6;
mybox2.depth = 9;
// display volume of first box
mybox1.volume();
// display volume of second box
mybox2.volume();
}
}
Volume is 3000.0
Volume is 162.0
154. Look closely at the following two lines of code:
mybox1.volume();
mybox2.volume();
The first line here invokes the volume( ) method on mybox1. That
is, it calls volume( ) relative to the mybox1 object, using the object’s
name followed by the dot operator.
Thus, the call to mybox1.volume( ) displays the volume of the box
defined by mybox1, and the call to mybox2.volume( ) displays the
volume of the box defined by mybox2.
Each time volume( ) is invoked, it displays the volume for the
specified box.
155. There is something very important to notice inside the volume( )
method: the instance variables width, height, and depth are
referred to directly, without preceding them with an object name or
the dot operator.
When a method uses an instance variable that is defined by its
class, it does so directly, without explicit reference to an object and
without use of the dot operator.
Once this invocation has occurred, the object is known. Thus,
within a method, there is no need to specify the object a second
time.
This means that width, height, and depth inside volume( )
implicitly refer to the copies of those variables found in the object
that invokes volume( ).
156. Returning a Value
A better way to implement volume( ) is to have it compute the volume of
the box and return the result to the caller.
class Box
{
double width;
double height;
double depth;
// compute and return volume
double volume()
{
return width * height * depth;
}
}
157. class BoxDemo4
{
public static void main(String args[])
{
Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
// assign values to mybox1's instance variables
mybox1.width = 10;
mybox1.height = 20;
mybox1.depth = 15;
158. //assign different values to mybox2’s instance variables
mybox2.width = 3;
mybox2.height = 6;
mybox2.depth = 9;
// get volume of first box
vol = mybox1.volume();
System.out.println("Volume is " + vol);
//System.out.println("Volume is" + mybox1.volume());
// get volume of second box
vol = mybox2.volume();
System.out.println("Volume is " + vol);
}
}
159. As you can see, when volume( ) is called, it is put on the right side of
an assignment statement.
On the left is a variable, in this case vol, that will receive the value
returned by volume( ). Thus, after
vol = mybox1.volume();
executes, the value of mybox1.volume( ) is 3,000 and this value then is
stored in vol.
There are two important things to understand about returning values:
The type of data returned by a method must be compatible with
the return type specified by the method. For example, if the return
type of some method is boolean, you could not return an integer.
The variable receiving the value returned by a method (such as vol,
in this case) must also be compatible with the return type specified
for the method.
160. Adding a Method That Takes Parameters
Parameters allow a method to be generalized. That is, a
parameterized method can operate on a variety of data and/or be
used in a number of slightly different situations.
class Box
{
double width;
double height;
double depth;
// compute and return volume
double volume()
{
return width * height * depth;
}
161. // sets dimensions of box
void setDim(double w, double h, double d)
{
width = w;
height = h;
depth = d;
}
}
class BoxDemo5
{
public static void main(String args[])
{
Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
162. // initialize each box
mybox1.setDim(10, 20, 15);
mybox2.setDim(3, 6, 9);
// get volume of first box
vol = mybox1.volume();
System.out.println("Volume is " + vol);
// get volume of second box
vol = mybox2.volume();
System.out.println("Volume is " + vol);
}
}
163. As you can see, the setDim( ) method is used to set the dimensions of
each box. For example, when
mybox1.setDim(10, 20, 15);
is executed, 10 is copied into parameter w, 20 is copied into h, and 15
is copied into d.
Inside setDim( ) the values of w, h, and d are then assigned to width,
height, and depth, respectively.
164. 5. Constructors
A constructor initializes an object immediately upon creation. It
has the same name as the class in which it resides and is
syntactically similar to a method.
Constructors look a little strange because they have no return type,
not even void.
This is because the implicit return type of a class’ constructor is the
class type itself.
It is the constructor’s job to initialize the internal state of an object
so that the code creating an instance will have a fully initialized,
usable object immediately.
165. We can rework the Box example so that the dimensions of a box are
automatically initialized when an object is constructed. To do so, replace
setDim( ) with a constructor.
class Box
{
double width;
double height;
double depth;
// This is the constructor for Box.
Box()
{
System.out.println("Constructing Box");
width = 10;
height = 10;
depth = 10;
}
166. // compute and return volume
double volume()
{
return width * height * depth;
}
}
class BoxDemo6
{
public static void main(String args[])
{
// declare, allocate, and initialize Box objects
Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
167. // get volume of first box
vol = mybox1.volume();
System.out.println("Volume is " + vol);
// get volume of second box
vol = mybox2.volume();
System.out.println("Volume is " + vol);
}
}
Constructing Box
Constructing Box
Volume is 1000.0
Volume is 1000.0
168. As you can see, both mybox1 and mybox2 were initialized by the
Box( ) constructor when they were created.
Since the constructor gives all boxes the same dimensions, 10 by 10 by
10, both mybox1 and mybox2 will have the same volume.
The println( ) statement inside Box( ) is for the sake of illustration
only.
Most constructors will not display anything. They will simply
initialize an object.
169. Before moving on, let’s reexamine the new operator. As you know,
when you allocate an object, you use the following general form:
class-var = new classname ( );
Now you can understand why the parentheses are needed after the
class name. What is actually happening is that the constructor for the
class is being called. Thus, in the line
Box mybox1 = new Box();
new Box( ) is calling the Box( ) constructor.
When you do not explicitly define a constructor for a class, then Java
creates a default constructor for the class.
This is why the preceding line of code worked in earlier versions of
Box that did not define a constructor
170. The default constructor automatically initializes all instance variables
to their default values, which are zero, null, and false, for numeric
types, reference types, and boolean, respectively.
The default constructor is often sufficient for simple classes, but it
usually won’t do for more sophisticated ones.
Once you define your own constructor, the default constructor is no
longer used.
171. Parameterized Constructors
While the Box( ) constructor in the preceding example does initialize
a Box object, it is not very useful—all boxes have the same
dimensions.
What is needed is a way to construct Box objects of various
dimensions.
The easy solution is to add parameters to the constructor. As you can
probably guess, this makes it much more useful.
For example, the following version of Box defines a parameterized
constructor that sets the dimensions of a box as specified by those
parameters.
172. class Box
{
double width;
double height;
double depth;
// This is the constructor for Box
Box(double w, double h, double d)
{
width = w;
height = h;
depth = d;
}
// compute and return volume
double volume()
{
return width * height * depth;
}
}
173. class BoxDemo7
{
public static void main(String args[])
{
// declare, allocate, and initialize Box objects
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box(3, 6, 9);
double vol;
// get volume of first box
vol = mybox1.volume();
System.out.println("Volume is " + vol);
174. // get volume of second box
vol = mybox2.volume();
System.out.println("Volume is " + vol);
}
}
Volume is 3000.0
Volume is 162.0
175. As you can see, each object is initialized as specified in the parameters
to its constructor. For example, in the following line,
Box mybox1 = new Box(10, 20, 15);
The values 10, 20, and 15 are passed to the Box( ) constructor when
new creates the object.
Thus, mybox1’s copy of width, height, and depth will contain the
values 10, 20, and 15, respectively.
176. 6. this Keyword
Sometimes a method will need to refer to the object that invoked it.
To allow this, Java defines the this keyword.
this can be used inside any method to refer to the current object.
That is, this is always a reference to the object on which the method
was invoked.
You can use this anywhere a reference to an object of the current
class’ type is permitted.
177. Following are the usage of this keyword
this can be used to get the current object.
this can be used to invoke current object's method.
this() can be used to invoke current class constructor
this can be passed as a parameter to a method call.
this can be passed as a parameter to a constructor.
this can be used to return the current object from the method.
178. Instance Variable Hiding
As you know, it is illegal in Java to declare two local variables with
the same name inside the same or enclosing scopes.
Interestingly, you can have local variables, including formal
parameters to methods, which overlap with the names of the class
instance variables.
However, when a local variable has the same name as an instance
variable, the local variable hides the instance variable.
179. class Student
{
int rollno;
String name;
String dept;
//Constructor
Student(int rollno,String name,String dept)
{
rollno=rollno;
name=name;
dept=dept;
}
180. void display()
{
System.out.println(rollno+" "+name+" "+dept);
}
}
class Same
{
public static void main(String args[])
{
Student s1=new Student(501,"Sarath","CSE");
Student s2=new Student(502,"Karthik","CSE");
s1.display();
s2.display();
}
}
181. 0 null null
0 null null
In the above example, parameters (formal arguments) and instance
variables are same.
182. class Student
{
int rollno;
String name;
String dept;
//Constructor
Student(int rollno,String name,String dept)
{
this.rollno=rollno;
this.name=name;
this.dept=dept;
}
184. 501 Sarath CSE
502 Karthik CSE
this keyword is used to distinguish local variable and instance
variable.
185. 7. Garbage Collection
In java, garbage means unreferenced objects.
Garbage Collection is process of reclaiming the runtime unused
memory automatically. In other words, it is a way to destroy the
unused objects.
To do so, we were using free() function in C language and delete()
in C++. But, in java it is performed automatically. So, java provides
better memory management.
186. Advantages of Garbage Collection
It makes java memory efficient because garbage collector removes
the unreferenced objects from heap memory.
It is automatically done by the garbage collector(a part of JVM) so
we don't need to make extra efforts.
Increases memory efficiency and decreases the chances for memory
leak.
187. How can an object be unreferenced?
1. Set null to object reference which makes it able for garbage
collection
Demo demo = new Demo();
demo = null; // ready for garbage collection
2. Non-reference an object by setting new reference to it which
makes it able for garbage collection.
Demo demo = new Demo();
Demo demo2 = new Demo();
demo2 = demo // referring object
3. Anonymous object does not have any reference so if it is not in
use, it is ready for the garbage collection.
new Demo();
188. The finalize( ) Method
Sometime an object will need to perform some specific task before it
is destroyed such as closing an open connection or releasing any
resources held.
To handle such situation finalize() method is used.
The finalize() method is called by garbage collection thread before
collecting object.
Its the last chance for any object to perform cleanup utility.
189. The finalize( ) method has this general form:
protected void finalize( )
{
// finalization code here
}
Here, the keyword protected is a specifier that limits access to
finalize( ).
190. It is defined in java.lang.Object class, therefore it is available to all the
classes.
It is declare as proctected inside Object class.
It gets called only once by a Daemon thread named GC (Garbage
Collector) thread.
Daemon thread is a low priority thread (in context of JVM) that runs in
background to perform tasks such as garbage collection (gc) etc.
Java gc() method is used to call garbage collector explicitly. However gc()
method does not guarantee that JVM will perform the garbage collection.
It only request the JVM for garbage collection. This method is present in
System and Runtime class.
191. public class ExampleGC
{
public static void main(String[] args)
{
Test t = new Test();
t=null;
System.gc();
}
public void finalize()
{
System.out.println("Garbage Collected");
}
}
192. 8. Overloading Methods
In Java, it is possible to define two or more methods within the
same class that share the same name, as long as their parameter
declarations are different.
When this is the case, the methods are said to be overloaded, and
the process is referred to as method overloading.
Method overloading is one of the ways that Java supports
polymorphism.
193. When an overloaded method is invoked, Java uses the type and/or
number of arguments as its guide to determine which version of
the overloaded method to actually call.
Thus, overloaded methods must differ in the type and/or number
of their parameters.
While overloaded methods may have different return types, the
return type alone is insufficient to distinguish two versions of a
method.
When Java encounters a call to an overloaded method, it simply
executes the version of the method whose parameters match the
arguments used in the call.
195. // Overload test for two integer parameters
void test(int a, int b)
{
System.out.println("a and b: " + a + " " + b);
}
// Overload test for a double parameter
double test(double a)
{
System.out.println("double a: " + a);
return a*a;
}
}
196. class Overload
{
public static void main(String args[])
{
OverloadDemo ob = new OverloadDemo();
double result;
// call all versions of test()
ob.test();
ob.test(10);
ob.test(10, 20);
result = ob.test(123.25);
System.out.println("Result of ob.test(123.25): " + result);
}
}
197. No parameters
a: 10
a and b: 10 20
double a: 123.25
Result of ob.test(123.25): 15190.5625
As you can see, test( ) is overloaded four times. The first version
takes no parameters, the second takes one integer parameter, the
third takes two integer parameters, and the fourth takes one double
parameter.
The fact that the fourth version of test( ) also returns a value is of no
consequence relative to overloading, since return types do not play
a role in overload resolution.
198. Automatic type conversions
When an overloaded method is called, Java looks for a match
between the arguments used to call the method and the method’s
parameters.
However, this match need not always be exact.
In some cases, Java’s automatic type conversions can play a role in
overload resolution.
200. // Overload test for a double parameter
void test(double a)
{
System.out.println("Inside test(double) a: " + a);
}
}
class OverloadType
{
public static void main(String args[])
{
OverloadDemo ob = new OverloadDemo();
201. int i = 88;
ob.test();
ob.test(10, 20);
ob.test(i); // this will invoke test(double)
ob.test(123.2); // this will invoke test(double)
}
}
No parameters
a and b: 10 20
Inside test(double) a: 88
Inside test(double) a: 123.2
202. As you can see, this version of OverloadDemo does not define
test(int).
Therefore, when test( ) is called with an integer argument inside
Overload, no matching method is found.
However, Java can automatically convert an integer into a double,
and this conversion can be used to resolve the call.
Therefore, after test(int) is not found, Java elevates i to double and
then calls test(double). Of course, if test(int) had been defined, it
would have been called instead.
Java will employ its automatic type conversions only if no exact
match is found.
203. Overloading Constructors
In addition to overloading normal methods, you can also overload
constructor methods.
In fact, for most real-world classes that you create, overloaded
constructors will be the norm, not the exception.
class Box
{
double width;
double height;
double depth;
204. // constructor used when all dimensions specified
Box(double w, double h, double d)
{
width = w;
height = h;
depth = d;
}
// constructor used when no dimensions specified
Box()
{
// use -1 to indicate an uninitialized box
width = -1;
height = -1;
depth = -1;
}
205. // constructor used when cube is created
Box(double len)
{
width = height = depth = len;
}
// compute and return volume
double volume()
{
return width * height * depth;
}
}
206. class OverloadCons
{
public static void main(String args[])
{
// create boxes using the various constructors
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box();
Box mycube = new Box(7);
double vol;
// get volume of first box
vol = mybox1.volume();
System.out.println("Volume of mybox1 is " + vol);
207. // get volume of second box
vol = mybox2.volume();
System.out.println("Volume of mybox2 is " + vol);
// get volume of cube
vol = mycube.volume();
System.out.println("Volume of mycube is " + vol);
}
}
Volume of mybox1 is 3000.0
Volume of mybox2 is -1.0
Volume of mycube is 343.0
208. 9. Using Objects as Parameters
So far, we have only been using simple types as parameters to methods.
However, it is both correct and common to pass objects to methods.
For example, consider the following short program:
class Test
{
int a, b;
Test(int i, int j)
{
a = i;
b = j;
}
209. // return true if o is equal to the invoking object
boolean equalTo(Test o)
{
if(o.a == a && o.b == b)
return true;
else
return false;
}
}
210. class PassOb
{
public static void main(String args[])
{
Test ob1 = new Test(100, 22);
Test ob2 = new Test(100, 22);
Test ob3 = new Test(-1, -1);
System.out.println("ob1 == ob2: " + ob1.equalTo(ob2));
System.out.println("ob1 == ob3: " + ob1.equalTo(ob3));
}
}
ob1 == ob2: true
ob1 == ob3: false
211. As you can see, the equalTo( ) method inside Test compares two
objects for equality and returns the result.
That is, it compares the invoking object with the one that it is
passed.
If they contain the same values, then the method returns true.
Otherwise, it returns false.
Notice that the parameter o in equalTo( ) specifies Test as its type.
Although Test is a class type created by the program, it is used in
just the same way as Java’s built-in types.
212. Use of object parameters involves constructors
One of the most common uses of object parameters involves
constructors.
Frequently, you will want to construct a new object so that it is
initially the same as some existing object.
To do this, you must define a constructor that takes an object of its
class as a parameter.
For example, the following version of Box allows one object to
initialize another:
213. class Box
{
double width;
double height;
double depth;
// Notice this constructor. It takes an object of type Box
Box(Box ob)
{
// pass object to constructor
width = ob.width;
height = ob.height;
depth = ob.depth;
}
214. // constructor used when all dimensions specified
Box(double w, double h, double d)
{
width = w;
height = h;
depth = d;
}
// constructor used when no dimensions specified
Box()
{
// use -1 to indicate an uninitialized box
width = -1;
height = -1;
depth = -1;
}
215. // constructor used when cube is created
Box(double len)
{
width = height = depth = len;
}
// compute and return volume
double volume()
{
return width * height * depth;
}
}
216. class OverloadCons2
{
public static void main(String args[])
{
// create boxes using the various constructors
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box();
Box mycube = new Box(7);
// create copy of mybox1
Box myclone = new Box(mybox1);
double vol;
217. // get volume of first box
vol = mybox1.volume();
System.out.println("Volume of mybox1 is " + vol);
// get volume of second box
vol = mybox2.volume();
System.out.println("Volume of mybox2 is " + vol);
// get volume of cube
vol = mycube.volume();
System.out.println("Volume of cube is " + vol);
218. // get volume of clone
vol = myclone.volume();
System.out.println("Volume of clone is " + vol);
}
}
Volume of mybox1 is 3000.0
Volume of mybox2 is -1.0
Volume of cube is 343.0
Volume of clone is 3000.0
219. A Closer Look at Argument Passing
In general, there are two ways that a computer language can pass
an argument to a subroutine.
The first way is call-by-value.
This approach copies the value of an argument into the formal
parameter of the subroutine.
Therefore, changes made to the parameter of the subroutine have
no effect on the argument.
220. The second way an argument can be passed is call-by-reference.
In this approach, a reference to an argument (not the value of the
argument) is passed to the parameter.
Inside the subroutine, this reference is used to access the actual
argument specified in the call.
This means that changes made to the parameter will affect the
argument used to call the subroutine.
222. class CallByValue
{
public static void main(String args[])
{
Test ob = new Test();
int a = 15, b = 20;
System.out.println("a and b before call: " + a + " " + b);
ob.meth(a, b);
System.out.println("a and b after call: " + a + " " + b);
}
}
a and b before call: 15 20
a and b after call: 15 20
223. Objects are passed through their references
When you pass an object to a method, objects are passed by what is
effectively call-by-reference.
Keep in mind that when you create a variable of a class type, you
are only creating a reference to an object.
Thus, when you pass this reference to a method, the parameter that
receives it will refer to the same object as that referred to by the
argument.
This effectively means that objects act as if they are passed to
methods by use of call-by-reference.
Changes to the object inside the method do affect the object used as
an argument. For example, consider the following program:
224. class Test
{
int a, b;
Test(int i, int j)
{
a = i;
b = j;
}
// pass an object
void meth(Test o)
{
o.a *= 2;
o.b /= 2;
}
}
225. class PassObjRef
{
public static void main(String args[])
{
Test ob = new Test(15, 20);
System.out.println("ob.a and ob.b before call: " + ob.a + " " + ob.b);
ob.meth(ob);
System.out.println("ob.a and ob.b after call: " + ob.a + " " + ob.b);
}
}
ob.a and ob.b before call: 15 20
ob.a and ob.b after call: 30 10
226. 10. Returning Objects
A method can return any type of data, including class types that
you create.
For example, in the following program, the incrByTen( ) method
returns an object in which the value of a is ten greater than it is in
the invoking object.
230. 11. Recursion
Java supports recursion. Recursion is the process of defining something
in terms of itself.
As it relates to Java programming, recursion is the attribute that allows a
method to call itself.
A method that calls itself is said to be recursive.
The classic example of recursion is the computation of the factorial of a
number.
The factorial of a number N is the product of all the whole numbers
between 1 and N. For example, 3 factorial is 1 × 2 × 3 ×, or 6.
231. class Factorial
{
// this is a recursive method
int fact(int n)
{
int result;
if(n==1)
return 1;
result = fact(n-1) * n;
return result;
}
}
232. class Recursion
{
public static void main(String args[])
{
Factorial f = new Factorial();
System.out.println("Factorial of 3 is " + f.fact(3));
System.out.println("Factorial of 4 is " + f.fact(4));
System.out.println("Factorial of 5 is " + f.fact(5));
}
}
Factorial of 3 is 6
Factorial of 4 is 24
Factorial of 5 is 120
233. 12. Static and Final Keywords
Understanding Static
There will be times when you will want to define a class member
that will be used independently of any object of that class.
Normally, a class member must be accessed only in conjunction
with an object of its class.
However, it is possible to create a member that can be used by
itself, without reference to a specific instance.
To create such a member, precede its declaration with the keyword
static.
234. When a member is declared static, it can be accessed before any
objects of its class are created, and without reference to any object.
You can declare both methods and variables to be static. The most
common example of a static member is main( ). main( ) is declared
as static because it must be called before any objects exist.
Instance variables declared as static are, essentially, global variables.
When objects of its class are declared, no copy of a static variable is
made.
Instead, all instances of the class share the same static variable.
235. Methods declared as static have several restrictions:
• They can only directly call other static methods.
• They can only directly access static data.
• They cannot refer to this or super in any way.
If you need to do computation in order to initialize your static
variables, you can declare a static block that gets executed exactly
once, when the class is first loaded.
The following example shows a class that has a static method, some
static variables, and a static initialization block:
236. class UseStatic
{
static int a = 3;
static int b;
static void meth(int x)
{
System.out.println("x = " + x);
System.out.println("a = " + a);
System.out.println("b = " + b);
}
238. Outside of the class in which they are defined, static methods and
variables can be used independently of any object.
To do so, you need only specify the name of their class followed by the
dot operator.
For example, if you wish to call a static method from outside its class,
you can do so using the following general form:
classname.method( )
Here, classname is the name of the class in which the static method is
declared. As you can see, this format is similar to that used to call non-
static methods through object reference variables.
A static variable can be accessed in the same way—by use of the dot
operator on the name of the class.
239. class StaticDemo
{
static int a = 42;
static int b = 99;
static void callme()
{
System.out.println("a = " + a);
}
}
240. class StaticByName
{
public static void main(String args[])
{
StaticDemo.callme();
System.out.println("b = " + StaticDemo.b);
}
}
a = 42
b = 99
241. Introducing final
A field can be declared as final. Doing so prevents its contents from
being modified, making it, essentially, a constant.
This means that you must initialize a final field when it is declared.
You can do this in one of two ways: First, you can give it a value
when it is declared. Second, you can assign it a value within a
constructor.
The first approach is the most common.
Here is an example:.
242. final int FILE_NEW = 1;
final int FILE_OPEN = 2;
final int FILE_SAVE = 3;
final int FILE_SAVEAS = 4;
final int FILE_QUIT = 5;
Subsequent parts of your program can now use FILE_OPEN, etc.,
as if they were constants, without fear that a value has been
changed.
It is a common coding convention to choose all uppercase
identifiers for final fields, as this example shows.
243. In addition to fields, both method parameters and local variables
can be declared final.
Declaring a parameter final prevents it from being changed within
the method.
Declaring a local variable final prevents it from being assigned a
value more than once.
The keyword final can also be applied to methods, but its meaning
is substantially different than when it is applied to variables.
244. 13. Nested and Inner Classes
It is possible to define a class within another class; such classes are
known as nested classes.
The scope of a nested class is bounded by the scope of its enclosing
class.
Thus, if class B is defined within class A, then B does not exist
independently of A.
A nested class has access to the members, including private
members, of the class in which it is nested.
245. However, the enclosing class does not have access to the members
of the nested class.
A nested class that is declared directly within its enclosing class
scope is a member of its enclosing class.
It is also possible to declare a nested class that is local to a block.
There are two types of nested classes: static and non-static. A static
nested class is one that has the static modifier applied.
Because it is static, it must access the non-static members of its
enclosing class through an object.
246. That is, it cannot refer to non-static members of its enclosing class
directly.
Because of this restriction, static nested classes are seldom used.
The most important type of nested class is the inner class. An inner
class is a non-static nested class.
It has access to all of the variables and methods of its outer class
and may refer to them directly in the same way that other non-
static members of the outer class do.
247. class Outer
{
int outer_x = 100;
void test()
{
Inner inner = new Inner();
inner.display();
}
// this is an inner class
class Inner
{
void display()
{
System.out.println("display: outer_x = " + outer_x);
}
}
}
249. In the program, an inner class named Inner is defined within the
scope of class Outer.
Therefore, any code in class Inner can directly access the variable
outer_x.
An instance method named display( ) is defined inside Inner. This
method displays outer_x on the standard output stream.
The main( ) method of InnerClassDemo creates an instance of class
Outer and invokes its test( ) method.
That method creates an instance of class Inner and the display( )
method is called.