Download presentation
Presentation is loading. Please wait.
1
Java Basics M Taimoor Khan taimoorkhan@ciit-attock.edu.pk
2
Content Java language syntax Structure of Java programs Creating a Java program Documentation, why it is important Reading and writing from the console 2
3
Important Java is cAsE sensitive A semicolon “;” is used to indicate the end of a statement ( VB is different) Braces, “{“ & “}” are often used to create blocks of code 3
4
All code is written in a class A program will have at least one class and may use many other classes Classes are written by programmers Many classes are supplied with Java The program will start running with a class that contains a special method public static void main (String args[]) 4
5
Hello World 5 Execution will start with this statement
6
Comments 6 Comments may appear in blocks /** indicates start of comment */ indicates end of comment All methods should have comments that describe what they do Comments may also appear at the end of a line of code These comments use two slashes // to indicate a start of comment.
7
Defining a class 7 A class is defined by the key word “class” A class has a name. By convention starts with a capital letter and each new word is also capitalised (Look up the syntax rules) The first brace indicates where the class begins The last “matching” brace indicates where the class ends
8
Methods 8 Methods have an opening brace “{“ A class will normally contain many methods. These are the same as functions and procedures. This class has one method Methods start with a method signature. A closing brace “}” A number of statements each terminated by a “;” semicolon
9
import java.awt.*; /** * A square that can be manipulated and that draws itself on a canvas. * * @author Michael Kolling and David J. Barnes * @version 2008.03.30 */ public class Square { private int size; private int xPosition; private int yPosition; private String color; private boolean isVisible; /** * Create a new square at default position with default color. */ public Square() { size = 30; xPosition = 60; yPosition = 50; color = "red"; isVisible = false; } 9 Knows about other classes Have a class header that describes the class Have instance variables that hold data for this class Have a constructor Have many methods, not shown
10
Structure within a class Attributes, (what the class knows about) o Defined as variables Data used within the class only (use private) Can be used externally (provide accessor or make public) Operations, (the things the class does) Defined in methods (Functions & Procedures) o Classified as accessor methods or mutator methods Constructors o Used when we create objects based on a class 10 Should be cohesive
11
A Program works with many classes 11 A class that has a public static void main method The place where the program starts Other classes written by you classes written by other programmers classes that come with Java Breaking a program into classes helps us understand our programs better. “Like behaviour” can be grouped into classes
12
Variables Most programs manipulate data. Data must be stored while the program is running We use variables to store the data Look at the tutorial o http://java.sun.com/docs/books/tutorial/java/nutsandbolts/v ariables.html http://java.sun.com/docs/books/tutorial/java/nutsandbolts/v ariables.html 12
13
String Class String str = "abc"; How do I find out more about String? Look at the API o http://java.sun.com/javase/6/docs/api/java/lang/String.ht ml http://java.sun.com/javase/6/docs/api/java/lang/String.ht ml Do a java tutorial o http://java.sun.com/docs/books/tutorial/java/data/strings.html http://java.sun.com/docs/books/tutorial/java/data/strings.html 13 “Strings are constant; their values cannot be changed after they are created”
14
Declaring variables and assigning values Local variables – (located inside methods) boolean result = true; char capitalC = 'C'; byte b = 100; short s = 10000; int i = 100000; Instance variables - (inside a class, but not in a method) public char capitalC = 'C'; private byte b = 100; Parameters public void myProcedure(int i,String r) 14 Note use of public or private – Very Important. We will talk about scope often. This method has two parameters
15
Variables – Different types Instance Variables o Available when an object is created Class Variables o Declared with a static modifier Local Variables o Declared within methods Parameters o Declared as part of method signature 15 These are different from instance variables Arguments are sent to methods
16
Arrays 16 Source: Sun Java Tutorials
17
Functions & Parameters 17
18
for Loop 18
19
while Loop 19
20
do Loop 20
21
if 21
22
if …else 22
23
switch 23
24
if ….elseif …..else 24
25
Reading from the Console Independent research o http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html o http://www.cs.utexas.edu/users/ndale/Scanner.html http://www.cs.utexas.edu/users/ndale/Scanner.html o http://www.java-tips.org/java-se-tips/java.util/scanning-text-with- java.util.scanner-3.html http://www.java-tips.org/java-se-tips/java.util/scanning-text-with- java.util.scanner-3.html o http://www.cs.utk.edu/~cs365/examples/datacheck.html http://www.cs.utk.edu/~cs365/examples/datacheck.html 25
26
The “.”, what does it mean? "the dot " connects classes and objects to members. o when you are connecting an object reference variable to a method. o when you are connecting a class name to one of its static fields. “An example of this is the dot between "System" and "out" in the statements we use to print stuff to the console window. System is the name of a class included in every Java implementation. It has an object reference variable that points to a PrintStream object for the console. So, "System.out.println( "text") invokes the println() method of the System.out object.” Source : http://www.bfoit.org/Intro_to_Programming/JavaOperators.html http://www.bfoit.org/Intro_to_Programming/JavaOperators.html 26 Are there any other uses of the “.” operator?
27
Pass by value or by reference? http://javadude.com/articles/passbyvalue.htmIntroducti on http://javadude.com/articles/passbyvalue.htmIntroducti on o Pass-by-value “The actual parameter (or argument expression) is fully evaluated and the resulting value is copied into a location being used to hold the formal parameter's value during method/function execution. That location is typically a chunk of memory on the runtime stack for the application (which is how Java handles it) o Pass-by-reference “The formal parameter merely acts as an alias for the actual parameter. Anytime the method/function uses the formal parameter (for reading or writing), it is actually using the actual parameter.” Java is strictly pass-by-value 27
28
Scope What does scope mean? o Restricting what code has access to variables, methods and classes Why do we care about it? o Poor scope means our programs may become corrupted or vulnerable May not run as expected How do we set scope? o Use an access modifier public, private o By being careful about where we declare variables and methods how we declare variables and methods More on this later 28
29
How can you document your code? Class Header Class Name Variables names Method names Method headers Comments in code ( Block and Line ) Comment to Javadoc standard Run Javadoc software 29
30
Why is documentation vital? Maintenance of code is very costly o Improve or modify o Fix bugs What is obvious now will not be in one months time Makes the code easier to reuse Makes the code easier to understand Gets you better marks 30
31
References http://java.sun.com/docs/books/tutorial/getStarted/application/i ndex.html http://java.sun.com/docs/books/tutorial/getStarted/application/i ndex.html http://java.sun.com/docs/books/tutorial/java/nutsandbolts/array s.html http://java.sun.com/docs/books/tutorial/java/nutsandbolts/array s.html http://java.sun.com/docs/books/tutorial/java/nutsandbolts/datat ypes.html http://java.sun.com/docs/books/tutorial/java/nutsandbolts/datat ypes.html http://webclass.superquest.net/apjava/JavaNotes Escape sequences o http://www.cerritos.edu/jwilson/cis_182/Language_Resources/ Java_Escape_Sequences.htm http://www.cerritos.edu/jwilson/cis_182/Language_Resources/ Java_Escape_Sequences.htm Code Conventions o http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html Java 6 API o http://java.sun.com/javase/6/docs/api/ http://java.sun.com/javase/6/docs/api/ 31
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.