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
Inductor kickback 1 of 2
I want to talk about a new example of an inductor circuit, and we have one shown here where this inductor is now controlled by a switch. This is a push button switch that we move in and out, and this metal plate here will touch these two contacts and comp…
The Evolution of Ancient Egypt's Pyramids | Lost Treasures of Egypt
NARRATOR: The West Bank of the River Nile, home to the world’s most iconic monuments, the mighty pyramids of Giza. The pyramids once housed the bodies of the pharaohs. But though ancient Egyptian civilization lasted for nearly 3,000 years, its kings only …
Natural hazards | Earth and society | Middle school Earth and space science | Khan Academy
Before I go to a new place, I tried to do some thorough research about it. What do I want to do there? What’s the weather forecast? What’s the chance that it might erupt? I learned this the hard way recently while preparing for a trip to Mount Rainier Nat…
Direction Game | Brain Games
It’s time to look at one of the most important brain functions of all: memory. Of course, to get to our next location, we’ll need directions, so let’s play a direction game. Here’s a simple memory test. Pay attention to the directions we give you. Betwee…
Khanmigo essay feedback demo | Introducing Khanmigo | Khanmigo for students | Khan Academy
Hey, this is Sarah from KH Academy, and I’m going to show you how to use our “Give Feedback on My Academic Essay” activity from Kigo. Like all other Kigo activities, you can get here from your AI activities page under the right section of the menu. When …
The Dark Side of Kindness: 8 Unexpected Drawbacks | STOICISM INSIGHTS
In a world that celebrates kindness and generosity, we often overlook the potential downsides of being excessively giving. Today, we’ll explore the unexpected ways in which boundless generosity can harm you. In this episode, we’ll delve into the darker si…