Arrays
An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed. You've seen an example of arrays already, in the main
method of the "Hello World!" application. This section discusses arrays in greater detail.
Looping in Java
Loops are another essential part of modern programming languages, with no exception in Java. Their premise is simple: the computer executes some operation or group of operations until told to do otherwise or stop.
The loop comes in several variations, each with their own slight variance in structure and purpose: for loops, while loops, do-while loops, and and the brand new for-each loop. While each has differences, all consist of loosely the same structure:
- A loop entry, or the point where the loop begins execution
- A loop test, where the computer tests to see if the loop should begin, repeat, or skip to the next command in the program
- Iteration, or a single cycle through the loop's group of operations
- A termination condition, or the specified situation or event that a loop reaches when it's time to stop looping
Java Control Structures
switch (the Java case statement)
//note: shown using char. //Primitive types char, byte, short, or int // can be used in the switch.
switch (charToCheck) { case 'A' : System.out.print('A'); break; //Print 'A' if charToCheck = 'A' case 'B' : System.out.print('B'); break; //Print 'B' if charToCheck = 'B' default : System.out.print('D'); //Print 'D' if charToCheck does // not equal 'A' or 'B' }
//note: if neither break were there, // and if charToCheck = 'A', // the code would print ABDif else
if (x > 5) { System.out.println("x is greater than 5"); } else { System.out.println("x is not greater than 5"); }StringsStrings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects.METHODSThe Java platform provides the
String
class to create and manipulate strings.
1.concat()
2.length()
3.IndexOf()
4.LastIndexOf()
5.CharAt()
6.equals()
7.equalsIgnoreCase()
8.StartsWith()
9.endswith()
10.toUpperCase()
11.toLowerCase()
12.replace()
13.Substring()
Using JOptionPane
import javax.swing.JOptionPane;
indicates that we want to import the class JOptionPane from the javax.swing package.
We can also write this as,
import javax.swing.*;
The statement,
name = JOptionPane.showInputDialog("Please enter your name");
creates a JOptionPane input dialog, which will display a dialog with a message, a
textfield and an OK button as shown in the figure. This returns a String which we will
save in the name variable.
2.Introduction to java programming
● Java– was created in 1991– by James Gosling et al. of Sun Microsystems.– Initially called Oak, in honor of the tree outside Gosling's window, its namewas changed to Java because there was already a language called Oak.– The original motivation for Java● The need for platform independent language that could be embedded in variousconsumer electronic products like toasters and refrigerators.– One of the first projects developed using Java● a personal hand-held remote control named Star 7.– At about the same time, the World Wide Web and the Internet were gainingpopularity. Gosling et. al. realized that Java could be used for Internetprogramming.● The Java technology is:– A programming language– A development environment– An application environment– A deployment environment● Java Virtual Machine (JVM)– an imaginary machine that is implemented by emulating software on a realmachine– provides the hardware platform specifications to which you compile all Javatechnology code● Bytecode– a special machine language that can be understood by the Java VirtualMachine (JVM)– independent of any particular computer hardware, so any computer with aJava interpreter can execute the compiled Java program, no matter whattype of computer the program was compiled on● Garbage collection thread– responsible for freeing any memory that can be freed. This happensautomatically during the lifetime of the Java program.– programmer is freed from the burden of having to deallocate thatmemory themselves
No comments:
Post a Comment