Language Basics Code Samples
Learn basic language constructs to start writing applications with the Java programming language.
Arrays
- 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.
- 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.
- 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.
- 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.
- 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.
Control Flow
- 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.
*BreakWithLabelDemo.java searches for a value in a two-dimensional array. For supporting information see The Java Tutorial: Nuts and Bolts.
- ContinueDemo.java steps through a string buffer and checks each letter. For supporting information see The Java Tutorial: Nuts and Bolts.
- ContinueWithLabelDemo.java uses nested loops to search for a substring within another string. For supporting information see The Java Tutorial: Nuts and Bolts.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
Date & Time
- 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.
- 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.
Operators
- ArithmeticDemo.java shows you how to perform mathematical operations with numbers. For supporting information see The Java Tutorial: Nuts and Bolts.
Packages
Variables
- 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.