Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

JAVA Variables Declaring Variables Over writing Variables Data types Displaying contents of a Variable Naming a Variable Strings.
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
CS324e - Elements of Graphics and Visualization A Little Java A Little Python.
Lecture 1: Comments, Variables, Assignment. Definitions The formal (human-readable) instructions that we give to the computer is called source code The.
Written by: Dr. JJ Shepherd
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
Road Map Introduction to object oriented programming. Classes
Methods Liang, Chapter 4. What is a method? A method is a way of running an ‘encapsulated’ series of commands. System.out.println(“ Whazzup ”); JOptionPane.showMessageDialog(null,
Java Syntax Primitive data types Operators Control statements.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Hello, world! Dissect HelloWorld.java Compile it Run it.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Introduction to Methods
Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Hello AP Computer Science!. What are some of the things that you have used computers for?
Moving from C to Java. The Java Syntax We Know Java Types Some of the types we know and love are still there  Integer: byte, short, char, int, long.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
2: Everything is an Object You Manipulate Objects Using References Primitives Arrays in Java Scoping You Never Destroy Objects Creating New Data Types:
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Objects, Classes and Syntax Dr. Andrew Wallace PhD BEng(hons) EurIng
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
Newport Robotics Group 1 Tuesdays, 6:30 – 8:30 PM Newport High School Week 4 10/23/2014.
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
Aside: Running Supplied *.java Programs Just double clicking on a *.java file may not be too useful! 1.In Eclipse, create a project for this program or.
Written by: Dr. JJ Shepherd
Classes - Intermediate
OOP Basics Classes & Methods (c) IDMS/SQL News
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Eastside Robotics Alliance / Newport Robotics Group 1 T/Th, 6:30 – 8:30 PM Big Picture School Day 3 · 10/9/2014.
Java Fundamentals MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh.
Information and Computer Sciences University of Hawaii, Manoa
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
Objects as a programming concept
Chapter 2 Basic Computation
Chapter 7 User-Defined Methods.
Console Output, Variables, Literals, and Introduction to Type
Java Programming by Ramiro Rocha.
CS180 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Programming Language Concepts (CIS 635)
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Lesson 2: Building Blocks of Programming
An Introduction to Java – Part I, language basics
Suggested self-checks: Section 7.11 #1-11
Just Enough Java 17-May-19.
Methods/Functions.
References Revisted (Ch 5)
JAVA. Java is a high-level programming language originally developed by Sun Microsystems and released in Java runs on a variety of platforms, such.
Presentation transcript:

Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace

Primitive Variables Primitive variables are variables of the most basic type, and the most commonly used primitive variables are the following: int – This is an integer. Use this type of variable to store integers ranging from -2,147,483,648 to 2,147,483,647. If you need to store larger numbers, use primitive type long. Google java long for more info. boolean – This represents a true or false value. Use this type of variable to make conditions that can be set and changed from true to false and vice versa. char – This represents a single uni-code character, like ‘a’. This variable is useful for initials or hotkeys.

Primitive Variables: Declaration To declare a primitive variable, the syntax is: (modifiers)(access-modifier)(static or non-static)(variable type)(variable name); An example: (This is declaring a private and static variable called numberOfPillows of type int) private static int numberOfPillows; If you want to assign a variable a value during declaration, simply do this: private static int numberOfPillows = 9;

Classes: Runner Classes As you may know, every program you create is called a class. Normally, classes don’t have any executable code, that is, the code in a class will not produce output of any sort. The classes made to run programs are called runner classes and have a main routine. A class that displays Hello World in output goes like this: public class HelloWorldClass { public static void main(String[] args) { System.out.println(“Hello World”); }

Classes: Runner Classes Cont. Let’s break this down. public class HelloWorldClass This declares a class called HelloWorldClass. public static void main(String[] args) This is the main routine. Don’t worry about all the void and args and String[], you will understand that later. System.out.println(“Hello World”); This prints out the message Hello World. In short, Runner Classes have a main routine that executes all the good stuff.

Methods: Custom Methods You may have used some of Java’s built in methods, such as the println() method, which usually takes a String or an object parameter. But the people who wrote Java can’t write everything, can they? Like many other programming languages, java, gives the programmer total control, that is, allowing you, the programmer, to create your own methods and use them. A custom method can either be a method that needs input (parameters), or a method that just performs an action.

Methods: Examples To make your own custom method, the syntax is as follows: ( ) { } An example of a method that adds 7 to a int passed into it: public int addSeven(int parameterInt) { return parameterInt + 7; }

Methods: Implementation Using custom methods is just like using Java methods, but remember that addSeven() returns an int. Let’s say that you wanted to use addSeven in a main routine to add 7 to an int variable…(this is not practical, i’m just using it as an example) public static void main(String[] args) { int myNum = 4; //Declare myNum and set it equal to 4. myNum = addSeven(myNum); //In here, you return myNum + 7 and assign it to myNum. //At the end of this code, myNum equals 11. }

Static Methods and Variables Static means only one copy, that is, can only be used inside the class. Static methods can be accessed like so in an outside class:. ( ) Like: Math.sqrt(9); Static variables are accessed the same way:. Like: myAnimalClass.numberOfDogs;

Classes: Object Classes There is another type of class, and I like to call it the object class. This is a class that programmers create in which they want to use it to create variables with different properties, which we will explain very soon. In object classes, there are things called constructors, which are basically methods that are used in the creation of a new object with the type of your class. The syntax for a constructor goes like this: ( ) { }

Classes: Object Classes Cont. Let’s say you want to make an object class called Car. Cars have wheels, doors, weight and color. We will use those in the constructor when writing our Car class.

Classes: An Example Here is the source code the object class mentioned earlier: Car. public class Car { int numWheels, numDoors, amtWeight; String typeColor; public Car (int wheels, int doors, int weight, String color) { numWheels = wheels; numDoors = doors; amtWeight = weight; typeColor = color; } As you can see, everytime you create a new Car, the values you pass into the constructor as params will be saved into the class (or non-static) variables defined in Car. What are class variables? I will discuss that very soon. But how do we create a new Car? Hmm…

Object Variables You can make your own variables and make those variables’ types your class, as we know from Object Classes. Let’s take the previous example, the object class Car. We will use its constructor to create a new object variable with type Car! This is how you create an Object variable. It follows most of the original syntax to create a new variable. private static Car myHonda = new Car(4,4,2250,”Beige”); The only difference is the new Car(). new calls the constructor to do its work in the Car class. After this, you have a Car variable named myHonda, and it has 4 wheels, 4 doors, weights 2250 pounds and is Beige colored!

Class (Non-Static) Variables and Methods The last thing that you need to know are class (or non-static variables and methods), and knowing this will wrap things up. Static means one copy, so non-static must mean more than one copy. When you declare a variable or a method and you do not put static, that makes it a class variable/method. This means that each time you create a object variable of that class like Car, there will be a copy of the class variables and methods for each Car that you create! To summarize, static variables and methods can only be used in the class, and can be accessed outside of the class by going like.. Class variables and methods CANNOT be used in the class, and can only be used when a object variable (or just object) of that class is created.

That’s All!