Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

This section gives a whirlwind intro to very basic Java syntax: loops, if statements, use of "main", strings, arrays, indenting, and so forth. Remember that this tutorial series is primarily intended for developers that have used another high-level language, so this section almost certainly moves too quickly for those without previous programming experience.

See http://courses.coreservlets.com/Course-Materials/java.html for the complete tutorial series (Programming with Java 8) and associated code. That site also lets you download PDF files of each lecture for saving or printing, and includes exercises and exercise solutions for each of the topics.

5.
7
Eclipse: Making Projects
• Main steps
– File  New  Project 
Java  Java Project
• Pick any name
– If you plan to run
from command line
• Choose sources/classes
in same project folder

6.
8
Eclipse: Creating Classes
• Main steps
– R-click on project  New  Class
– Choose a capitalized class name
(e.g., Class1 or MyFirstClass)
• You can have Eclipse make
“main” when class is created, but
easier to use shortcut to insert it later
• Eventually you will make package
(subdirectory) first, then put class there
Packages explained in upcoming section
• Alternative
– Can also copy/rename existing class

9.
11
Packages
• Idea
– Packages are subdirectories used to avoid name conflicts
– Java class must have “package subdirname;” at the top
• But Eclipse puts this in automatically when you right-click on a package
and use New  Class
• Naming conventions
– Package names are in all lower case
– Some organizations use highly nested names
• com.companyname.projectname.projectcomponent
• Creating packages in Eclipse
– R-click project, New  Package (use all-lowercase name by convention)
– Then R-click package and New  Class (use capitalized name by convention)

10.
12
HelloWorld with Packages (in src/mypackage folder)
package mypackage;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, world (using packages)");
}
}
Run from Eclipse in normal manner: R-click, Run As  Java Application. Running from the command line is a pain: you must go
to parent directory and do “java mypackage.HelloWorld”. Run from Eclipse and it is simple to use packages.

14.
16
Array Basics: The length Field
• The length variable tells you number of array elements
– Gives the number of elements in any array
String[] names = { "John", "Jane", "Juan" };
• names.length is 3
• But last entry ("Juan") is names[2], not names[3]
– For command-line arguments
• In main, args.length gives the number of command-line arguments
• Unlike in C/C++, the name of the program is not inserted into the command-line
arguments

15.
17
Command-line Arguments
• Are useful for learning and testing
– Command-line args are slightly helpful for beginner’s practice
– But, programs given to end users should almost never use command-line arguments
• They should pop up a GUI to collect input
• Eclipse has poor support
– Entering command-line args via Eclipse is more trouble than it is worth
– So, to test with command-line args:
• Save the file in Eclipse (causing it to be compiled)
• Navigate to folder on desktop (not within Eclipse)
• Open command window (Start icon, Run…  cmd)
• Type “java Classname arg1 arg2 …”

29.
31
Defining Multiple Methods in Single Class
public class LoopTest {
public static void main(String[] args) {
String[] test = {"This", "is", "a", "test"};
listEntries(test);
listNums1(5);
listNums2(6);
listNums3(7);
}
public static void listEntries(String[] entries) {...}
public static void listNums1(int max) {...}
public static void listNums2(int max) {...}
public static void listNums3(int max) {...}
}
These methods say “static” because they are called directly from “main”.
In the upcoming sections on OOP, we will explain what “static” means and
why most regular methods do not use “static”. But for now, just note that
methods that are directly called by “main” must say “static”.

32.
Indentation: Number of Spaces and Placement of Braces is a
Matter of Taste
OK
blah;
blah;
for(...) {
blah;
blah;
for(...) {
blah;
blah;
}
}
OK
blah;
blah;
for(...) {
blah;
blah;
for(...) {
blah;
blah;
}
}
OK
blah;
blah;
for(...)
{
blah;
blah;
for(...)
{
blah;
blah;
}
}Some organizations or projects make coding-style documents that all developers in the organization or on the project should follow. For example, the one for
Google can be found at https://google.github.io/styleguide/javaguide.html. Although I personally follow almost all of those stylistic conventions, I am skeptical
about how necessary or even valuable it is to enforce this on everyone in an organization or project.

34.
36
If Statements: One or Two Options
• Single option
if (booleanExpression) {
statement1;
...
statementN;
}
• Two options
if (booleanExpression) {
...
} else {
...
}
The value inside parens must be strictly boolean (i.e.,
true or false), unlike C, C++, and JavaScript.
A widely accepted best practice is to use the braces even
if there is only a single statement inside the if or else.

40.
42
Strings: Basics
• Overview
– String is a real class in Java, not an array of characters as in C++
– The String class has a shortcut method to create a new object: just use double quotes
String s = "Hello";
• Differs from normal classes, where you use new to build an object
• Use equals to compare strings
– Never use == to test if two Strings have same characters!

69.
72
Summary
• Basics
– Loops, conditional statements, and array access is similar to C/C++
• But additional “for each” loop: for(String s: someStrings) { … }
– Indent your code for readability
– String is a real class in Java
• Use equals, not ==, to compare strings
• Allocate arrays in one step or in two steps
– If two steps, loop down array and supply values
• Use Math.blah() for simple math operations
– Math.random, Math.sin, Math.cos, Math.pow, etc.
• Simple input from command window
– Use command line for strings supplied at program startup
– Use JOptionPane or Scanner to read values after prompts
• Neither is very important for most real-life applications