0% found this document useful (0 votes)
33 views

1.introduction To Java

Uploaded by

PRAVEEN
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
33 views

1.introduction To Java

Uploaded by

PRAVEEN
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 23
Introduction to Java Vasily Strelnikov 1U-FIELD DELIVERY Cee ee ee eee eet Course Goals In this course, you learn how to implement application logic using Java SE: + Describe the object-oriented programming approach + Explain Java syntax and coding conventions + Use Java constructs and operators Use core Java APIs, such as Collections, Streams, IO, and Concurrency + Deploy Java SE applications Vasily Strelnikov SENIOR PRINCIPAL INSTRUCTOR, OU-FIELO DELIVERY ORACLE UNIVERSITY Ea Cony ©2020, Oracle anos alias, i ahs reserve, Audience The target audience includes those who: + Have some non-Java programming experience and want to learn Java, + Have basic knowledge of Java and want to improve it + Prepare for the Java SE 11 Certification exams CCopynght ©2020, Gace andor aise. Al igs asd, Course Structure Lesson 4: Introduction to Java Lesson 2: Primitive Types, Operators, and Flow Control Statements Lesson 3: Text, Date, Time, and Numeric Objects Lesson 4: Classes and Objects Lesson 5: Improved Class Design Lesson 6: Inheritance Lesson 7: Interfaces Lesson 8: Arrays and Loops Lesson 9: Collections Lesson 10: Nested Classes and Lambda Expressions Lesson 11: Java Streams API Lesson 12: Handle Exceptions and Fix Bugs Lesson 13: Java IO API Lesson 14: Java Concurrency and Multithreading Lesson 15: Java Modules z Extras: + Appendix A: Annotations + Appendix B: JDBC API + Appendix C: Security + Appendix D: Generics + Appendix E: Cloud Deployment Course Practices During the course practice sessions, you: + Explore the features of Java language Apply the knowledge gained throughout the course to develop a product management application The practice environment uses: + JDK11 + JShell + NetBeans 11 Copyright© 2020, Orel andre flats, Al ras reserved. Lesson Objectives After completing this lesson, you should be able to: + Discover Java language origins and use-cases + Explain Java portability and provider neutrality + Explain object-oriented concepts + Describe Java syntax and coding conventions + Create Java class with main method + Compile and execute a Java application Copyright ©2020, Ola ands afstes. Al igh reserved What Is Java? + General purpose programming language similar to C and C++ + Object-Oriented and Platform-Independent * Originally designed in 1995 for use in consumer electronics + Modem uses include writing applications for internet of things, Cloud computing, and so on. & + This course covers Java SE (Standard Edition) version 11 => = ‘Java Editions: Java Card - Smart card Edition Java ME ~ Micro Edition Java SE - Standard Edition Java MP - Micro Profile Java EE - Enterprise Edition +} Java SE is the base edition on which other editions are based. ava Copyright © 2020, Oracle andlor its affitates. All rights reserved, How Java Works? Java is a platform-independent programming language. + Java source code is written as plain text . java files. * Source code is compiled into byte-code . class files for JVM. + Java Virtual Machine must be installed on a target computer. + JVM executes your application by translating Java byte-code instructions to platform-specific code. Ratna oe (QS public class Helloworld( p public static void main(String{} args) ( = System.out.printin("Hello World"); , ’ (001200) 320011 dU | davac Hentoworia. java “Note: A Java program has to be compiled only once to work on any platform! e SANE comp ©2101 Oa ern Mi Classes Class and Object are two key object-oriented concepts. Java code is structured with classes. + Class represents a type of thing or a concept, such as Dog, Cat, Ball, Person. + Each class defines what kind of information (attributes) it can store: - ADog could have a name, color, size. ~ ABall would have type, material, and so on. + Each class defines what kind of behaviors (operations) containing program logic (algorithms) itis capable of: ~ ADog could bark and fetch a Ball. - ACat could meow but is not likely to play fetch. class Person ( class Dog { void play() { String name; Dog dog = new Dog()s fetch (Ball ball) dog.name = "Rex"; ball. find(); Bali ball = new Ball(); ball.chew()? dog. fetch (ball); , , } e tat + Objects are covered next Comyn ©2020, rate andor flats. Al igh reseed, ° Objects ‘An Object is a specific instance (example of) a Class. + Each object would be capable of having specific values for each attribute defined by a Es class that represents its type. For example: ~ A dog could be called Fido, and be brown and small EK. - Another could be called Rex, and be orange and big Fido Ff + To operate on an object, you can reference it using a variable of a relevant type. + Each object would be capable of behaviors defined by a class that represents its type: Rex - Atrun time, objects invoke operations upon each other to execute program logic. class Person { class Dog { void play() ( String name; Dog dog = new Dog); fetch (Ball ball) { dog.name = "Rex"; ball. find( Ball ball = new Ball(); ball.chew(); dog. fetch (ball) + ) ) ) ) _—— Java ceca Oude nr ane Mewes Inheritance You can reuse (inherit) attributes and behaviors across class hierarchy. * Classes can form hierarchical relationships. + Superclass represents a more generic, parent type (living organism). + Superclasses define common attributes and behaviors (eat, propagate). + Asubclass represents a more specific, child type (animal, plant, and so on). + There could be any number of levels in the hierarchy, from very generic to very 9 specific child types (dog, cat, and so on). + Subclasses inherit all attributes and behaviors from their parents. + Subclasses can define more specific attributes and behaviors (swim, fly). class Animal extends LivingOrganisn { // generic attributes and behaviours B inherited class Dog extends Animal ( 7/ specific attributes and behaviours — Nz Copyright © 2020, Oracle andlor is afiiatos. All rights reserved. W Java APIs Java Development Kit (JDK) provides hundreds of classes for various programming purposes: To represent basic data types, for example, String, LocalDateTime, BigDecimal, and so on To manipulate collections, for example, Enumeration, ArrayList, HashMap, and so on To handle generic behaviors and perform system actions, for example, System, Object, Class, and so on To perform input/output (V/O) operations, for example, FileInput Stream, FileOutput Stream, and so on Many other API classes are used to access databases, manage concurrency, enable network communications, execute scripts, manage transactions, security, logging, build graphical user interfaces, and so on ++ Application Programming Interface (API) is a term that describes a collection of classes that are designed to serve a common purpose. +FAll Java APIS are thoroughly documented for each version of the 6 language. For example, Java 11 documentation can be found at: = Java httos://docs.oracle.com/en/iavaljavase/‘ 1/docs/api/ = fs ENE) 0 andor sass Ml gs reserved 2 Java Keywords, Reserved Words, and a Special Identifier Keywords available since 1.0 ry eee ce oe Keywords no longer in use else extends try enum Keywords added in 1.2 continue interface catch module ae F real class finally requires Keywords added in 1.4 for static throw transitive Keywords added in 5.0 do final throws xports to Keywords added in 9.0 while return uses switch transient provides case void we Reserved words for literals values Gefault byte ae private short identi i protected int synchronized true Special identifier added in 10 Preis me sleeeaa =n. import char goto pull Package float const abstract double stricttp var eS Notes ++ Keywords and Literals cannot be used as identifiers (names of classes, variables, methods, and so on). +f Actual use and meaning of these keywords and literals are covered later in this course. Java Copyright © 2020, Oracle andior its affiliates. All rights reserved. 8 Java Naming Conventions + Java is case-sensitive; Dog is not the same as dog. + Package name is a reverse of your company domain name, plus the naming system adopted within your company. + Class name should be a noun, in mixed case with the first letter of each word capitalized, + Variable name should be in mixed case starting with a lowercase letter; further words start with capital letters. + Names should not start with numeric characters (0-9), underscore _ or dollar $ symbols. + Constant name is typically written in uppercase with underscore symbols between words. + Method name should be a verb, in mixed case starting with a lowercase letter, further words start with capital letters. com.oracle.demos.animals | package: animals ShepheraDog Glass: Shepherd Dog 1 shepherdDog MIN_SIZE giveMePaw +Note: The use of the _ symbol as a first or only character in a variable name produces a compiler warning in Java 8 and an error in Java 9 onward. java) Commo 6ain Once tr tuian igiemned Java Basic Syntax Rules * All Java statements must be terminated with the " ; " symbol. * Code blocks must be enclosed with " {" and" }" symbols. + Indentations and spaces help readability, but are syntactically irrelevant. Package com.oracle.demos.animals; class Dog ( void fetch() { while (ball == null) { keepLooking () 7 ) void makeNoise() { if (ball wll) { droppall () ; ) else ( bark(); d } oe ‘Note: Example shows some constructs such as if/ese and while that are covered later in the course. Copyright © 2020, Orleans sins Alright reser, 16 Define Java Class + Class name is typically represented by one or more nouns, for example, Dog, SabreToothedCat, Person. *+ Class must be saved into a file with the same name as the class and extension . java. + Classes are grouped into packages. + Packages are represented as folders where class files are saved. + Package name is 4 reverse of your company domain name, plus a naming system adopted within your company. Example: com. oracle.demos, org.acme. something + Package and class name must form a unique combination. package Class ( 1 pr package con.oracle.denos.animals: elass Dog { 7 the rest of thie class code /somepath/con/oracle/demos/animals/Dog. java 5 2G ee a “‘FNote: If package definition is missing, class would belong to a "default package and would not be placed into any package folder. However, this is not a recommended practice. java CorMMCO2I Once mdr dee Noord Access Classes Across Packages To access a class in another package: + Prefix the class name with the package name + Use the import statement to import specific classes or the entire package content = The import of all classes from the java.1ang.* package is implicitly assumed. The example shows three alternative ways of referencing the class Dog in the package animals from the class Owner in the package people: package eninals; public class Dog { | , oe package people; package people: package people: Public class Owner {|| import aninals.Dog; import aninais.*; | ‘aninals.dog myDog: || public class Owner {| | public class Owner { i ‘Dog myDogs Dog myDogs ) ) SS Notes + Imports are not present in a compiled code. An import statement has no effect at runtime efficiency of the class. Itis ‘a simple convenience to avoid prefixing class name with package name throughout your source code. + Access modifiers (such as pub! ic) are explained in the following slide, 18 Use Access Modifiers Access modifiers describe the visibility of classes, variables, and methods. + public - Visible to any other class + protected - Visible to classes that are in the same package or to subclasses + - Visible only to classes in the same package + private - Visible only within the same class package ; package b; Unport .: import a. import .*; Public class ¥ extends x [ clase ( Public void dothings() { | package a7 x= new X(7 public class x ( Xaccess modifier> yt public ¥ yl; x , ee ae vy oe ¥3i 2 1937 14; rivate x vas ) T ) ee Notes —— +f means that no access modifier is explicitly set. + Subclass-Superclass relationship (use of the extends keyword) is covered later in the course. ++ Any nonprivate parts of your class should be kept as stable as possible, because changes of such code may adversely affect any number of other classes that may be using your code. Eve Copyright © 2020, Oracle and/or its affiiates. Al rights reserved. 9 - oe Create Main Application Class The main method is the entry point into your application. + Itis the starting point of program execution. + The method name must be called main. + Itmust be pub1ic. You intend to invoke this method from outside of this class. + Itmust be static. Such methods can be invoked without creating an instance of this class. + Itmust be voi. It does not return a value. It must accept array of String objects as the only parameter. (The name of this parameter "args" is irrelevant.) package demos; public class Whatever ( public static void main(String{] args) { // program execution starts here ) ) ++ Note: Use of static and void keywords and handling of arrays are covered later in the course. r Compile Java Program Compile classes with the javac Java compiler. + The -classpath or ~cp parameter points to locations of other classes that may be required to compile your code. + The -d parameter points to a path to store compilation result. (The compiler creates package subfolders with compiled class files in this path.) + Provide path to source code. Javac -cp /project/clas: es -d /project/classes /project/sources/denos/Whatever. java package demos; eae public class Whatever { Public static void main(String[] args) { // program execution starts here ) /project/classes/denos/thatever.class | Ea Copyrght ©2020, race ano ste. Alihts eserves, 2 Ow Execute Java Program Execute program using java executable Java Virtual Machine (JVM). + Specify classpath or -cp to point to folders where your classes are located. + Specify fully qualified class name. Use package prefix; do not use the .class extension. + Provide a space separated list of parameters after the class name. Access command-line parameters: package demos; + Use array object to access parameters. Public class Whatever (ay + Array index starts at 0 (frst parameter). Disteing raremiiciecaaniir System.out.println ("Hello “tparam1); demos Whatever Jo John "A Name” Jane java -cp /project/cl: — Hello John + Since Java 11, it is also possible to run single-file source code as if it is a compiled class. JVM will interpret your code, but no compiled class file would be created: a ) Comments and Documentation + Code Comments can be placed anywhere in your source code. + Documentation Comments: - May contain HTML markups Single iin - May contain descriptive tags prefixed with ¢ sing ~ Are used by the javadoc tool to generate documentation Pack: Class Whatever ee <—_ 6} The de Whatever} cla: a x * represents an example of demos Whatever Gavaaes » ascuaanbatiod oamecnt public class what extends The whatever class represents an example of docum = Javadoc -d sourcepath Author subpackages + Note: All APIs in the Java development kit are documented using the javadoc utility Summary In this lesson, you should have learned how to: + Discover Java language origins and use-cases + Explain Java portability and provider neutrality Explain object-oriented concepts + Describe Java syntax and coding conventions + Create a Java class by using the ma in method + Compile and execute a Java application

You might also like