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
Warren Buffett: A "Storm is Brewing" in the Banking Industry
Banks can take a lot of loan losses, but they can’t take something that wipes out their capital and expect the world to ignore that fact. If those rates change, let the person who bet that they wouldn’t change lose money. I mean, that’s if you make mistak…
The Secret Culture of Orcas | Podcast | Overheard at National Geographic
We’re up there just about the time that the polar night occurs, so that means it’s going to be dark essentially 24 hours a day. The sun never gets above the mountain peaks, so the light levels are extremely low. You’re in a boat in these little coves or, …
Techno Optimism, Explained
I cannot wait for the day where my daughter and son say, “Oh, 45 minutes to Tokyo? That’s so slow!” I can’t wait. Hello, this is Dalton, post-Michael, and today we’re going to talk about why we are optimistic about the future. So, to be clear, at YC, we…
Safari Live - Day 261 | National Geographic
This program features live coverage of an African safari and may include animal kills and carcasses. Viewer discretion is advised. The clouds are now starting to come close to each other; I can see that it might be overcasting any time soon here by the we…
15 Reasons Why Growing Up Rich is a Liability
If you grew up poor like we did, you certainly experienced firsthand what it’s like to never ask for anything, to be self-sufficient. But every single one of us, deep down, wished our family was rich. But you know how when you were little, you didn’t know…
6 Productivity Habits That Changed My Life
What’s up, Graham? It’s guys here. So, I would consider myself to be a bit of a productivity nerd. I meticulously schedule every hour of the day. I cut out all distractions. I created to-do lists the night before, and my day does not end until every item …