Java Tutorial - Colorado State University

Transcription

Java Tutorial

JAVA TUTORIALSimply Easy Learning by tutorialspoint.comtutorialspoint.com

ABOUT THE TUTORIALJava TutorialJava is a high-level programming language originally developed by Sun Microsystems and released in1995. Java runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX.This tutorial gives a complete understanding ofJava.This reference will take you through simple and practical approach while learning Java Programminglanguage.AudienceThis reference has been prepared for the beginners to help them understand the basic to advancedconcepts related to Java Programming language.PrerequisitesBefore you start doing practice with various types of examples given in this reference, I'm making anassumption that you are already aware about what is a computer program and what is a computerprogramming language?Copyright & Disclaimer Notice Allthe content and graphics on this tutorial are the property of tutorialspoint.com. Any content fromtutorialspoint.com or this tutorial may not be redistributed or reproduced in any way, shape, or formwithout the written permission of tutorialspoint.com. Failure to do so is a violation of copyright laws.This tutorial may contain inaccuracies or errors and tutorialspoint provides no guarantee regarding theaccuracy of the site or its contents including this tutorial. If you discover that the tutorialspoint.com siteor this tutorial content contains some errors, please contact us at webmaster@tutorialspoint.comTUTORIALS POINTSimply Easy Learning

Table of ContentJava Tutorial . 2Audience . 2Prerequisites . 2Copyright & Disclaimer Notice . 2Java Overview . 15History of Java: . 16Tools you will need: . 16What is Next? . 16Java Environment Setup . 17Setting up the path for windows 2000/XP: . 17Setting up the path for windows 95/98/ME: . 17Setting up the path for Linux, UNIX, Solaris, FreeBSD:. 17Popular Java Editors: . 18What is Next? . 18Java Basic Syntax . 19First Java Program: . 19Basic Syntax: . 20Java Identifiers: . 20Java Modifiers: . 21Java Variables: . 21Java Arrays:. 21Java Enums: . 21Example: . 21Java Keywords: . 22Comments in Java . 22Using Blank Lines: . 22Inheritance: . 22Interfaces: . 23What is Next? . 23Java Object & Classes . 24Objects in Java: . 24Classes in Java: . 25Constructors: . 25Singleton Classes . 26Implementing Singletons: . 26Example 1:. 26Example 2:. 26TUTORIALS POINTSimply Easy Learning

Creating an Object:. 27Accessing Instance Variables and Methods: . 27Example: . 28Source file declaration rules: . 28Java Package: . 29Import statements: . 29A Simple Case Study: . 29What is Next? . 31Java Basic Data Types . 32Primitive Data Types: . 32byte: . 32short: . 32int:. 33long: . 33float:. 33double: . 34boolean: . 34char: . 34Reference Data Types: . 34Java Literals: . 35What is Next? . 36Java Variable Types. 37Local variables:. 37Example: . 38Example: . 38Instance variables:. 38Example: . 39Class/static variables: . 40Example: . 40What is Next? . 41Java Modifier Types . 421. Java Access Modifiers . 42Default Access Modifier - No keyword: . 42Example: . 42Private Access Modifier - private: . 43Example: . 43Public Access Modifier - public: . 43Example: . 43Protected Access Modifier - protected: . 43TUTORIALS POINTSimply Easy Learning

Example: . 44Access Control and Inheritance: . 442. Non Access Modifiers . 44Access Control Modifiers: . 45Non Access Modifiers: . 45Access Control Modifiers: . 45Non Access Modifiers: . 45What is Next? . 46Java Basic Operators . 47The Arithmetic Operators: . 47The Relational Operators: . 48Example . 49The Bitwise Operators: . 49Example . 50The Logical Operators: . 51Example . 51The Assignment Operators: . 51Example: . 52Misc Operators . 53Conditional Operator (?:): . 53instanceof Operator: . 54Precedence of Java Operators: . 54What is Next? . 55Java Loop Control . 56The while Loop: . 56Syntax: . 56Example: . 56The do.while Loop: . 57Syntax: . 57Example: . 57The for Loop: . 58Syntax: . 58Example: . 58Enhanced for loop in Java: . 59Syntax: . 59Example: . 59The break Keyword: . 59Syntax: . 60Example: . 60TUTORIALS POINTSimply Easy Learning

The continue Keyword: . 60Syntax: . 60Example: . 60What is Next? . 61Java Decision Making . 62The if Statement: . 62Syntax: . 62Example: . 62The if.else Statement: . 63Syntax: . 63Example: . 63The if.else if.else Statement: . 63Syntax: . 63Example: . 64Nested if.else Statement: . 64Syntax: . 64Example: . 64The switch Statement: . 65Syntax: . 65Example: . 65What is Next? . 66Java Numbers . 67Example: . 67Number Methods: . 68xxxValue(). 69compareTo() . 70equals() . 71valueOf() . 72toString() . 73parseInt() . 74abs() . 75ceil() . 76floor() . 77rint() . 78round() . 78min() . 79max() . 80exp() . 81log() . 82TUTORIALS POINTSimply Easy Learning

pow() . 82sqrt() . 83sin() . 84cos() . 85tan() . 86asin() . 86acos() . 87atan() . 88atan2() . 89toDegrees(). 90toRadians() . 90random() . 91What is Next? . 92Java Characters . 93Example: . 93Example: . 93Escape Sequences:. 93Example: . 94Character Methods: . 94isLetter() . 95isDigit() . 96isWhitespace() . 96isUpperCase() . 97isLowerCase() . 98toUpperCase() . 99toLowerCase() . 99toString() . 100What is Next? . 101Java Strings . 102Creating Strings: . 102String Length: . 102Concatenating Strings: . 103Creating Format Strings: . 103String Methods: . 104char charAt(int index) . 106int compareTo(Object o) . 107int compareTo(String anotherString) . 108int compareToIgnoreCase(String str) . 109String concat(String str) . 110TUTORIALS POINTSimply Easy Learning

boolean contentEquals(StringBuffer sb) . 111static String copyValueOf(char[] data) . 112boolean endsWith(String suffix) . 113boolean equals(Object anObject) . 114boolean equalsIgnoreCase(String anotherString). 114byte getBytes() . 115byte[] getBytes(String charsetName) . 117void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) . 118int hashCode() . 119int indexOf(int ch) . 120int indexOf(int ch, int fromIndex) . 121int indexOf(String str) . 123int indexOf(String str, int fromIndex) . 124String intern() . 125int lastIndexOf(int ch). 126int lastIndexOf(int ch, int fromIndex) . 128int lastIndexOf(String str) . 129int lastIndexOf(String str, int fromIndex) . 131int length(). 132boolean matches(String regex). 133boolean regionMatches(boolean ignoreCase, int toffset, . 134String other, int ooffset, int len) . 134boolean regionMatches(int toffset, String other, int ooffset, int len) . 135String replace(char oldChar, char newChar) . 137String replaceAll(String regex, String replacement) . 137String replaceFirst(String regex, String replacement) . 138String[] split(String regex) . 139String[] split(String regex, int limit) . 141boolean startsWith(String prefix) . 142boolean startsWith(String prefix, int toffset) . 143CharSequence subSequence(int beginIndex, int endIndex) . 144String substring(int beginIndex) . 145String substring(int beginIndex, int endIndex) . 146char[] toCharArray() . 147String toLowerCase() . 148String toLowerCase(Locale locale) . 149String toString() . 150String toUpperCase() . 150String toUpperCase(Locale locale) . 151TUTORIALS POINTSimply Easy Learning

String trim() . 152static String valueOf(primitive data type x) . 153Java Arrays . 156Declaring Array Variables: . 156Example: . 156Creating Arrays:. 156Example: . 157Processing Arrays: . 157Example: . 157The foreach Loops:. 158Example: . 158Passing Arrays to Methods: . 158Returning an Array from a Method: . 159The Arrays Class: . 159Java Date and Time . 160Getting Current Date & Time . 161Date Comparison:. 161Date Formatting using SimpleDateFormat: . 161Simple DateFormat format codes: . 162Date Formatting using printf: . 162Date and Time Conversion Characters: . 164Parsing Strings into Dates: . 165Sleeping for a While: . 165Measuring Elapsed Time: . 166GregorianCalendar Class: . 166Example: . 168Java Regular Expressions. 170Capturing Groups: . 170Example: . 171Regular Expression Syntax: . 171Methods of the Matcher Class: . 172Index Methods: . 172Study Methods:. 173Replacement Methods: . 173The start and end Methods: . 173The matches and lookingAt Methods: . 174The replaceFirst and replaceAll Methods: . 175The appendReplacement and appendTail Methods: . 175PatternSyntaxException Class Methods: . 176TUTORIALS POINTSimply Easy Learning

Java Methods . 177Creating Method: . 177Example: . 178Method Calling:.

ABOUT THE TUTORIAL Java Tutorial Java is a high-level programming language originally developed by Sun Microsystems and released in 1995. Java runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX. This