Mastering Java Mastering Computer Science Series Editor: Sufyan bin Uzayr Mastering Java: A Beginner’s Guide Divya Sachdeva and Natalya Ustukpayeva Mastering Unreal Engine: A Beginner’s Guide Divya Sachdeva and Aruqqa Khateib Mastering UI Mockups and Frameworks: A Beginner’s Guide Mohamed Musthafa MC and Kapil Kishnani Mastering Ruby on Rails: A Beginner’s Guide Mathew Rooney and Madina Karybzhanova Mastering Sketch: A Beginner’s Guide Mathew Rooney and Md Javed Khan Mastering C#: A Beginner’s Guide Mohamed Musthafa MC, Divya Sachdeva, and Reza Nafim For more information about this series, please visit: https:// www.routledge.com/Mastering-Computer-Science/ book-series/MCS The “Mastering Computer Science” series of books are authored by the Zeba Academy team members, led by Sufyan bin Uzayr. Zeba Academy is an EdTech venture that develops courses and content for learners primarily in STEM fields, and offers education consulting to Universities and Institutions worldwide. For more info, please visit https:// zeba.academy Mastering Java A Beginner’s Guide Edited by Sufyan bin Uzayr First edition published 2022 by CRC Press 6000 Broken Sound Parkway NW, Suite 300, Boca Raton, FL 33487-2742 and by CRC Press 2 Park Square, Milton Park, Abingdon, Oxon, OX14 4RN CRC Press is an imprint of Taylor & Francis Group, LLC © 2022 Sufyan bin Uzayr Reasonable efforts have been made to publish reliable data and information, but the author and publisher cannot assume responsibility for the validity of all materials or the consequences of their use. The authors and publishers have attempted to trace the copyright holders of all material reproduced in this publication and apologize to copyright holders if permission to publish in this form has not been obtained. If any copyright material has not been acknowledged please write and let us know so we may rectify in any future reprint. Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmitted, or utilized in any form by any electronic, mechanical, or other means, now known or hereafter invented, including photocopying, microfilming, and recording, or in any information storage or retrieval system, without written permission from the publishers. For permission to photocopy or use material electronically from this work, access www. copyright.com or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923, 978-750-8400. For works that are not available on CCC please contact [email protected] Trademark Notice: Product or corporate names may be trademarks or registered trademarks and are used only for identification and explanation without intent to infringe. ISBN: 9781032134109 (hbk) ISBN: 9781032134086 (pbk) ISBN: 9781003229063 (ebk) DOI: 10.1201/9781003229063 Typeset in Minion by KnowledgeWorks Global Ltd. Contents About the Editor, xxv Chapter 1 ◾ I ntroduction to Java 1 WHAT IS JAVA? 2 How Does Java Work? 2 Why Is Java Important? 3 Java’s Technical Advantages Include 4 Java’s Advantages in the Workplace 5 THE JAVA PROGRAMMING LANGUAGE HAS THE FOLLOWING FEATURES 6 Simple and Well-Known 7 Compiled and Interpreted the Data 8 Independent of the Platform 9 Portable 10 Neutral in Architecture 10 Object-Oriented 10 Robust 11 Safe 11 Distributed 12 v vi ◾ Contents Interactive and Multi-Threaded 12 Outstanding Performance 13 Extensible and Dynamic 13 BASIC SYNTAX 14 What Is the Syntax of Java? 14 First Java Program 15 Syntax for Beginners 16 Identifiers in Java 17 Keywords in Java 18 Modifiers in Java 23 Variables in Java 23 Enums in Java 23 Literals in Java 24 Comments in Java 25 Blank Lines 27 Inheritance 27 Interfaces 27 Chapter 2 ◾ G etting Started with Java 29 HOW TO SETUP JAVA ON A WINDOWS COMPUTER 30 In Java, Here’s How to Establish Environment Variables: Classpath and Path 31 INSTALLING A JAVA INTEGRATED DEVELOPMENT ENVIRONMENT (IDE) 32 Internal Java Program Details 34 What Occurs throughout the Compilation Process? 34 What Occurs When the Program Is Running? 34 Contents ◾ vii TYPES OF PRIMITIVE DATA 35 Simple Programs to Write 35 Identifiers 37 Variables 38 Variable Declaration 38 Assignments Expressions and Assignment Statements 39 In a Single Step, Declare and Initialize Variables 40 Constants 40 Numerical Data Types and Operations 42 Numerical Operators 42 Numeric Literals 43 Integer Literals 43 Floating-Point Literals 44 Notations in Scientist 44 Arithmetic Expressions 44 Shortcut Operators 45 Numeric Type Conversions 46 Character Data Type and Operations 48 Unicode and ASCII Code 48 Special Character Escape Sequences 50 Casting between Char and Numeric Types 50 String Type 51 String Concatenation 51 viii ◾ Contents Converting a String to a Number 52 String to Integers Conversion 52 Strings to Doubles Conversion 52 Using the Console for Input 53 Using a Scanner to Getting Information 53 Documentation and Programming Style 54 Appropriate Comments and Style of Comments 54 Conventions for Naming 55 Spacing Lines and Proper Indentation 55 Block Styles 56 Errors in Programming 56 Syntax Problems “Compilation Errors” 56 Runtime Errors 57 Logic Errors 58 Debugging 58 CONTROL STRUCTURES 59 If 60 if Statement 60 if-else Statement 61 if-else-if Ladder 62 Nested if Statement 64 Switch Statement 66 Loop Statements 68 Jump Statements 73 Contents ◾ ix A GUIDE TO JAVA PACKAGES 76 Package Kinds in Java 77 Subpackages in Java 81 Chapter 3 ◾ O bject-Oriented Programming 83 JAVA OBJECT-ORIENTED PROGRAMMINGS (OOPs) CONCEPTS 84 OOPs 84 Object 85 Class 85 Inheritance 86 Polymorphism 86 Abstraction 86 Encapsulation 86 Coupling 86 Cohesion 87 Association 87 Aggregation 87 Composition 88 In Java, What Is the Difference between an Object and a Class? 88 Advantages of OOPs over Procedure-Oriented Programming Languages 88 Java Naming Conventions 89 Benefits of Java Naming Conventions 89 Class 90