View Source

h3.Language Basics Code Samples


Learn basic language constructs to start writing applications with the Java programming language.

* [#Arrays]
* [#Control Flow]
* [#Date and Time]
* [#Operators]
* [#Packages]
* [#Variables]

h5.Arrays

* [ArrayDemo.java|http://java.sun.com/docs/books/tutorial/java/data/examples/ArrayDemo.java] creates an array, puts some values in it, and displays the values. For supporting information see [The Java Tutorial: Object Basics and Simple Data Objects|http://java.sun.com/docs/books/tutorial/java/data/].

* [ArrayCopyDemo.java|http://java.sun.com/docs/books/tutorial/java/data/examples/ArrayCopyDemo.java] uses the arrayCopy method to copy some elements from one array to another. For supporting information see [The Java Tutorial: Object Basics and Simple Data Objects|http://java.sun.com/docs/books/tutorial/java/data/].

* [ArrayofArraysDemo.java|http://java.sun.com/docs/books/tutorial/java/data/examples/ArrayOfArraysDemo.java] creates an array and uses an initializer to populate it with four subarrays. For supporting information see [The Java Tutorial: Object Basics and Simple Data Objects|http://java.sun.com/docs/books/tutorial/java/data/].

* [ArrayofArraysDemo2.java|http://java.sun.com/docs/books/tutorial/java/data/examples/ArrayOfArraysDemo2.java] creates an array of arrays by explicitly writing code to create each subarray. For supporting information see [The Java Tutorial: Object Basics and Simple Data Objects|http://java.sun.com/docs/books/tutorial/java/data/].

* [ArrayofStringsDemo.java|http://java.sun.com/docs/books/tutorial/java/data/examples/ArrayOfStringsDemo.java] uses an array initializer to create an array containing three string objects and puts the strings in all lowercase letters. For supporting information see [The Java Tutorial: Object Basics and Simple Data Objects|http://java.sun.com/docs/books/tutorial/java/data/].

h5.Control Flow

* [BreakDemo.java|http://java.sun.com/docs/books/tutorial/java/nutsandbolts/examples/BreakDemo.java] contains a for loop that searches for a particular value within an array. For supporting information see [The Java Tutorial: Nuts and Bolts|http://java.sun.com/docs/books/tutorial/java/nutsandbolts/].

*[BreakWithLabelDemo.java|http://java.sun.com/docs/books/tutorial/java/nutsandbolts/examples/BreakWithLabelDemo.java] searches for a value in a two-dimensional array. For supporting information see [The Java Tutorial: Nuts and Bolts|http://java.sun.com/docs/books/tutorial/java/nutsandbolts/].

* [ContinueDemo.java|http://java.sun.com/docs/books/tutorial/java/nutsandbolts/examples/ContinueDemo.java] steps through a string buffer and checks each letter. For supporting information see [The Java Tutorial: Nuts and Bolts|http://java.sun.com/docs/books/tutorial/java/nutsandbolts/].

* [ContinueWithLabelDemo.java|http://java.sun.com/docs/books/tutorial/java/nutsandbolts/examples/ContinueWithLabelDemo.java] uses nested loops to search for a substring within another string. For supporting information see [The Java Tutorial: Nuts and Bolts|http://java.sun.com/docs/books/tutorial/java/nutsandbolts/].

* [DoWhileDemo.java|http://java.sun.com/docs/books/tutorial/java/nutsandbolts/examples/DoWhileDemo.java] shows how to use a do-while statement to continually execute a block of code while a condition remains true. For supporting information see [The Java Tutorial: Nuts and Bolts|http://java.sun.com/docs/books/tutorial/java/nutsandbolts/].

* [ForDemo.java|http://java.sun.com/docs/books/tutorial/java/nutsandbolts/examples/ForDemo.java] uses a for statement to iterate over the elements of an array and print them. For supporting information see [The Java Tutorial: Nuts and Bolts|http://java.sun.com/docs/books/tutorial/java/nutsandbolts/].

* [IfElseDemo.java|http://java.sun.com/docs/books/tutorial/java/nutsandbolts/examples/IfElseDemo.java] uses an if-else statement to assign a grade based on the value of a test score. For supporting information see [The Java Tutorial: Nuts and Bolts|http://java.sun.com/docs/books/tutorial/java/nutsandbolts/].

* [SwitchDemo.java|http://java.sun.com/docs/books/tutorial/java/nutsandbolts/examples/SwitchDemo.java] uses the switch statement to conditionally perform statements based on an integer expression. For supporting information see [The Java Tutorial: Nuts and Bolts|http://java.sun.com/docs/books/tutorial/java/nutsandbolts/].

* [SwitchDemo2.java|http://java.sun.com/docs/books/tutorial/java/nutsandbolts/examples/SwitchDemo2.java] illustrates how it can be useful to make a case statement terminate the flow of control. For supporting information see [The Java Tutorial: Nuts and Bolts|http://java.sun.com/docs/books/tutorial/java/nutsandbolts/].

* [WhileDemo.java|http://java.sun.com/docs/books/tutorial/java/nutsandbolts/examples/WhileDemo.java] shows how to use a while statement to continually execute a block of code while a condition remains true. For supporting information see [The Java Tutorial: Nuts and Bolts|http://java.sun.com/docs/books/tutorial/java/nutsandbolts/].

h5.Date & Time

* [June.java|http://java.sun.com/new2java/supplements/2002/June.java] demonstrates the limits of scope by finding out which month of the year is June. For supporting information see [New to Java Programming Supplement, Feb 2002|http://java.sun.com/new2java/supplements/2002/feb02.html].

* [TimeTest.java|http://java.sun.com/new2java/supplements/2001/TimeTest.java] shows you how to create a time stamp object that returns the current time. For supporting information see [New to Java Programming Supplement, November 2001|http://java.sun.com/new2java/supplements/2001/nov01.html].

h5.Operators

* [ArithmeticDemo.java|http://java.sun.com/docs/books/tutorial/java/nutsandbolts/examples/ArithmeticDemo.java] shows you how to perform mathematical operations with numbers. For supporting information see [The Java Tutorial: Nuts and Bolts|http://java.sun.com/docs/books/tutorial/java/nutsandbolts/].

h5.Packages

* [TestFileClass.java|http://java.sun.com/new2java/supplements/2002/TestFileClass.java] [New to Java Programming Supplement, Feb 2002|http://java.sun.com/new2java/supplements/2002/feb02.html].

h5.Variables

* [Cast.java|http://java.sun.com/new2java/supplements/2002/Cast.java] uses the {{instanceOf}} method to determine the type of objects in a list and shows how to cast the objects to their correct type before accessing the data. For supporting information see [New to Java Programming Supplement, March 2002|http://java.sun.com/new2java/supplements/2002/mar02.html].

----

The individuals who post here are part of the extended Sun Microsystems community and they might not be employed or in any way formally affiliated with Sun Microsystems. The opinions expressed here are their own, are not necessarily reviewed in advance by anyone but the individual authors, and neither Sun nor any other party necessarily agrees with them.

Copyright 1994-2009 Sun Microsystems, Inc.
Powered by Atlassian Confluence
Sun Guidelines on Public Discourse Privacy Policy Terms of Use Trademarks Site Map Employment Investor Relations Contact