1 / 110

Java Crash Course

naomi-olson
Download Presentation

Java Crash Course

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA. Java Crash Course Learn how to do stuff in 2 hours! A fun, slightly organized, but easy to follow guide by bitinvert.com

  2. Before we begin: • Install the JRE and the JDK • http://www.oracle.com/technetwork/java/javase/downloads/index.html • Install jGrasp • http://www.jgrasp.org/

  3. The key to learning • Understand what you are doing • If you don't, interrupt me and ask about it • This guide won’t teach you everything, but you will learn enough to teach yourselves or understand new concepts easily. • There is no assumed knowledge. What you need to understand will be explained. (I will say things that aren't on the slides)

  4. Computer basics - storage(useful information) • 1 bit is either a 1 or a 0 • 1 byte is 8 bits • 1 kilobyte is 1024 bits • Why? 210 = 1024 • 1 megabyte is 1024 kilobytes • 1 gigabyte is 1024 megabytes • Terabytes, petabytes, exabytes, and yottabytes follow

  5. Computer basics - storage(useful information) • To put this information in perspective: • A typical short novel is 1 megabyte • All of Shakespeare's work is 5 megabytes • A minute of high quality sound is 10 megabytes • A CD holds 700 MB • A DVD holds 4.7 GB • A Blu-Ray disc holds 25 GB source: http://searchstorage.techtarget.com/sDefinition/0,,sid5_gci944596,00.html

  6. Java basics - data types • boolean: either true or false • byte: min. value of -128 and max. of 127 • short: min. -32,768 and max. 32,767 • int (most widely used): • min. -2,147,483,648 and max. 2,147,483,647 • long: • min. -9,223,372,036,854,775,808and max. 9,223,372,036,854,775,807

  7. Java basics - data types • float: number with a decimal • double: number with decimal, holds more significant figures (more accurate) • char: a single character. Represented inside single quotes. (like 'A') Note: this is a very simplified explanation

  8. What makes a program run? • A programmer enters in source code • The source code is compiled to byte code • Byte code is source code converted to be far more efficient • Byte code is interpreted by Java to machine code • Computers can understand and execute machine code

  9. Important structural parts of source code • Type this: • The { and } control scope. • Everything inside can see everything else inside • This will make more sense later • Now save and compile it (but don't run it yet). Were there any errors?

  10. There were errors! • What does this mean for us and our blah? • We'll have to examine the error to find out.

  11. Open a web browser, and googlejava.lang • Do notuse any other search engines. • The first link is from oracle.com, the owners • The first link's title is java.lang. This looks relevant. Open this search result.

  12. The error is fundamental. Because the error is a part of java.lang, we can find a description of it on this java.lang page. • What is a method?

  13. What is a method? • A method is a set of instructions. • An example of a real-world method for brushing hair is: • To brush your hair, grab a brush by the handle with the bristles pointing down, place on your head, and stroke downward. • Computer programs need methods to be typed in a very specific way

  14. The main methodmain(String[] args) • Every runnable Java program needs something called a main method. • Let's add a main method to our code. • You might not know what all these words mean, but you will later today. • Save, compile, and run. What happens?

  15. Nothing happened. • (but you don't get an error!) • Why? Because your method doesn't contain any instructions for the computer. • Let's add some instruction: • System.out.println("stuff"); • Save, compile, and run your program.

  16. What do these words mean? • The purple words are keywords. They have specific uses for program structure. • A class is a file containing an object. • The object name here is IntroductionProgram • How about the stuff on the line creating the main method?

  17. What do these words mean? • How about the stuff on the line creating the main method? • public: everything with permission to the class has access to whatever is declared public • Let's explore a demonstration.

  18. Create a new Java file (File > New > Java) • Type this in, then compile it: • Modify your original class, IntroductionProgram, like this: (save/compile/run)

  19. Inside the main method, you call the sayHello method DemoOfPublic object (aka class) • (call means run)

  20. Now change the sayHello() method to private instead of public: • Run the InstructionProgram class again. • What happens?

  21. You get an error! • It's another java.lang. error. Let's find out what it means. • The error name is descriptive enough that we didn't have to look it up. • The sayHello method is private, so only the class it's in can use it. Make it public again, and save/compile • (run the main method again if you want)

  22. You know what public means, now • Everything with permission to the class has access to whatever is declared public

  23. Explaining static • Create a new class, called DemoOfStatic • Change the main class (IntroductionProgram): • Run the main class

  24. Remove the static modifier from sayGoodbye and compile. Run the main class again. • What happens?

  25. What happens? • You get an error. “Non-static method cannot be referenced from static context” • To run the method, you have to create an instance of the DemoOfStatic class. • This means that you have to create a DemoOfStatic object in order to use its methods • Let’s fix this

  26. Creating an instance of a class • Modify your main program. • Line 4 means: • We are creating space in the computer’s memory for a DemoOfStatic • The space will be called object • new DemoOfStatic() creates a default DemoOfStatic

  27. Creating an instance of a class • Now that an object is created in line 4, you can call methods from the object using dot notation. • Type the object’s variable name, then a dot, then the method that the object contains.

  28. Creating an instance of a class • Here is a way to think about an applied real-life situation: • Human is a class (object) • Human has methods to do things, like brushing hair or eating. • But you need a Human to brush hair or eat, you can't do that without having one. • The Human methods would be non-static

  29. Creating an instance of a class • Here is a way to think about an applied real-life situation: • Math is a class • With Math, you can take the absolute value of something without having to remember everything you learned about Math, ever. • The absolute value method would be static.

  30. You know what static means, now • static means that you don’t need to create an instance of the object to run the method.

  31. Explaining void • Modify your main program: • When you run the program, the main method is called automatically. The main method calls the answerToLife method.

  32. Explaining void • Remove void from line 6. Compile. What happens?

  33. Explaining void • You get an error. • When you have a method that is not void, it has to return a variable. • In Java, variables can hold various data types. • To return a variable from a method you have to say what type of variable you will return.

  34. Explaining void • Insert int to where void was. • Compile again. You will get another error: • This means that you have to specify what variable to return in the method.

  35. Explaining void • Change the answerToLife method like this: • Compile and run. What happens?

  36. It looks like nothing happens, but actually line 3 is being replaced by what is returned to it in line 8 • In this example it’s the same as writing • Neat, but what can we do with this?

  37. Modify your main method: • Line 3 creates a new int, called something, with the value that answerToLife returns minus two, all divided by ten. • Line 4 prints the int something.

  38. What if we want a method to take input and change it? • That’s what the parenthesis are for. Modify your main program like this:

  39. The calculateProfit method takes input. • You can pass an int to it • The input is named objectsSold • The initial cost to sell objects is 95 • The profit per object is 35, but we have to subtract the initial cost • The profit is then returned

  40. The main method creates an int called employeeProfit1 with the value calculated by passing 2 to the employeeProfit method • The int is then printed

  41. Modify your main method: • Methods can be reused any number of times.

  42. Real-life example of returning • A factory production method would take in raw materials:public int produceCan(int amountOfAluminum) • It would tell the size of the biggest can it can make back to the supplier of the resources:return sizeOfCan;

  43. You should now understand passing values • Input can be passed to a method inside parenthesis to modify output • Output is returned and can be used after the method is called

  44. Methods • You should now understand the basics of methods. • You've already been using one for a while:System.out.println("hello"); • println is the method, located in the out object of the System package.

  45. Creating variables • Type the variable type, then what to call the variable, then an = sign, and then what you want the variable to contain. • Variable names should start with a lowercase letter. After that, they can contain any letter or number. • theConventionIsToUseCamelCaseToSeparateWords • = is called the assignment operator

  46. Manipulating basic data types • Except for boolean, all basic data types can be changed using these operators: • + adds • - subtracts • * multiplies • / divides • % returns the remainder

  47. Manipulating basic data types • Handy tip: Usea <operator>= binstead ofa = a <operator> b • Example: a *= 4 is the same as a = a * 4 • Another handy tip:++ increments by 1-- decrements by 1 • Example: b++ is the same as b = b + 1

  48. Manipulating basic data types • Examples:1 + 1 (returns 2)3.0 - 2 (returns 1.0)5 * 10 (returns 50)5.0 / 10 (returns 0.5)10 % 3 (returns 1)

  49. Manipulating basic data types • The / operator is tricky, though • If you divide an integer by an integer, an integer is returned! • This means that the decimal is truncated (chopped off) • Example: 10 / 3 returns 3 (because it is 3 remainder 1, the 1 gets chopped off) • Moral of the story: be careful

  50. Order of operations • Java executes assignment from right to left • Everything else is executed left to right. • Math is executed in this order: • Parenthesis • Multiplication and division • Addition and subtraction • Assignments and math executions always return the result

More Related