yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

Java Lesson 1


5m read
·Nov 3, 2024

Hey guys, his mac kids in the one. Sorry about there not being so many videos I made in the last week because I've been busy with camp and stuff. But I'm going to start making more videos now that it's the weekend.

So, this is a Java lesson because I said I was going to make videos on Java, and all of you want to get to Objective-C as fast as possible, so I better hurry through Java. In this first Java lesson, we're not actually going to be doing any programs that are useful. Now, we are going to be getting ourselves into Java and knowing the basics of Java.

There are many programs that let you develop with Java. Since we have Xcode, I'm going to use Xcode. There are other programs that I like to use more, like something called Eclipse. If you have an error, Eclipse will have suggestions on how to fix it for you. It's just a great program in my opinion. You can get that, but for now, use Xcode.

So you'll go into Xcode, new project. Then, on the left side, under Mac OS X, there's Java, and then under that selection, you select Java Tool. You can do Java Tool or a Java application, but right now, we're going to use Java Tool. So I'm going to call it Java Tool, and on my desktop, I'm going to save it as Testing Java.

This is where you have to learn a bunch of important concepts. Java is a class-based language. It is full of classes. A class is like an object, and you can have multiple instances of one class. So you can have multiple int classes, multiple window classes, stuff like that. There’s a class called String, which is a piece of text. In the String, there's no limit to how long it could be. Like with C, we had character arrays that we normally made them 512 bytes long, but with Strings, they could be however long.

In this first lesson, I'm just going to be teaching simple stuff. So right under Source, there's going to be our file, and right here is going to be the source code. Now, this is going to be terminal apps for now. I'm sure we'll get into GUI stuff later, but you'd have to type up your interface or GUI, so that's not right now. In this nice code that they already put here, you'll notice a few things at the top.

There's something called import. Import imports code that someone else has made. java.util is code that Sun has made; that’s a bunch of cool little tools like random number generators, MD5 hash, or stuff like that. Import—there are a lot of things to import. Like if you import javax.swing, it will import all the GUI stuff for you, and there are tons of classes in java.util. So this import line just means that we're using java.util.

Next, you'll notice this public class. A class is an object, and this object is called Testing Java. Inside of that is a public static void. Public means, of course, that it is public to people outside of this class, and void means that it's a function. If that was supposed to return a String, we'd have to return a String here and stuff like that.

Then, between these little parentheses, are the parameters of this function, and this is an array of Strings called args that are the command line parameters. So all this might not make sense; it doesn't have to right now. All we know is that inside of this public static void main and the close curly brace, we're going to type all our code.

So, System.out.println will print a line of text in the console. If we run this, then I'm going to run, and the console says "Hello, World!" Great! So here's what we're going to do right now: we're going to take a String and another String and print out those two Strings together. So we're going to say String name = "Alex"; semicolon.

I'm going to have another String called phrase. I'm going to make that equal "Hello". So now here's how we do a System.out.println of this phrase and the name. This is much different than C. We would do print and then %s and stuff inside of the quotes and separate parameters.

With Java, it's much easier and, in my opinion, much more fun. You can use variable names with the plus sign, then another variable name or text, then a plus sign, and whatever. So right here, this would print out in order the String phrase, the String space, and the String name. Name is equal to "Alex" and phrase is equal to "Hello".

Of course, this is a space. So if I zoom in here, you can see more clearly that the plus indicates that we're adding a new piece of text or a new String. So I'm adding phrase, then I'm adding a space, then I'm adding name, and then I'm going to add a period right here. You can have another String variable name here, but it doesn't matter.

So now I can build it. When I run it, the console says "Hello Alex." So this is the result. Now, let's say you want to add two numbers. This is easy. The way to declare our numbers is you do int numberName. I'll make it i = 1; int i1 = 2. Now the way to multiply, add, divide, or subtract is you use the symbols. So we can say res = i * i1, and then we can use System.out.println(res).

Now here's the thing: res is an integer; System.out.println wants us to enter a String. So the way to make it think it's a String is to use + " ". Because once you add a String to it, it becomes a String automatically.

With this whole plus system, where you can just keep on adding Strings and variables together, you can also add into the output. You'll have the int value in there, but if it's just an int value, it doesn't know that it's a String. But if you add an empty String, like "", then it will think that what you've typed in as a String, and it will work.

So now it'll say "Hello Alex," and then it'll say "2" because 1 times 2 is 2. I'll run the console up here. This is "Hello Alex" and "2." Now say I make this 3 and I make this 2; it'll display "6."

Okay, and of course, when I'm doing i times i1, I can change this to be 5 times i1, so that would be 5 times 3. So this is how to multiply integers, how to combine Strings, and stuff like that.

Now say we want to make a String called output right here, and we want it to equal whatever is going to be printed out here. Whenever you're creating a String, you can use the same basic concept of pluses and stuff. So right here, we're going to have a String called output that's going to be "Hello Alex," and then we can just print out output right here.

So this is how that works. Instead of using stuff like printf and stuff like that, you can just add Strings, integers, anything together really to make it a String result.

So that right now is a simple lesson on how to declare Strings, integers, assign Strings and integers, multiply integers, and print stuff out. So I hope you just think Java's not that awful anymore. But definitely, this is a short lesson, and it could be longer. Just wait to learn more in the next few lessons.

So thanks for watching, my kids, and all that. Subscribe and goodbye.

More Articles

View All
Using matrices to manipulate data: Game show | Matrices | Precalculus | Khan Academy
We’re told in the beginning of each episode of a certain game show. Each contestant picks a certain door out of three doors. Then the game show host randomly picks one of the two prize bundles. After each round, each contestant receives a prize based on t…
If It’s Broke, Fix It | Port Protection
Salmon’s Stewart will have to clear both the main drain and the two beaver dams if they want to restore the water flow. If you got a foot of mud all the way around your pipe inlet, it’s got to reduce flow. It’d be like having a big water hair in your bath…
Charlie Munger: Be a Survivor, Not a Victim
Of course, feeling like it’s rather interesting to make change. Some people are victimized by other people, and if it weren’t for the indignation that that causes, we wouldn’t have the reforms that we need. But that truth is mixed with another. It’s very…
Warren Buffett's Timeless Investing Wisdom – 1988 Interview
To meet the wizard of Omaha, Warren Buffett, next on Adam Smith’s Money World. He doesn’t generally do interviews, but I called on him recently to get some of the wisdom and apherisms of Warren Buffett on the record. It is characteristic of Warren that he…
Imploding Drum
Today I’m at the University of Sydney with Dr. Phil, and we’re talking about the pressure that all of us are under. You are under a lot of pressure, probably 10,000 kg. 10,000 kg is pressing in on my whole body, all from all sides. Where does all this pre…
Spread of Islamic Culture | World History | Khan Academy
Hi man! Hi David! So, what we’re going to do in this video is talk about the cultural spread of Islam. Where are we and when are we right now? So, we are around the early 7th century, and here we see the Byzantine Empire, which at the time was still a co…