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
Ancient Egypt | Early Civilizations | World History | Khan Academy
[Narrator] In this video, we are going to give ourselves an overview of ancient Egypt, which corresponds geographically pretty closely to the modern day state of Egypt in northeast Africa. Now, the central feature in both ancient Egypt and in modern Egypt…
Neil deGrasse Tyson on a Dystopic Future | Breakthrough
It’s always been a curious fact to me that the most successful science fiction storytelling involves completely dystopic scenarios or finales, and all of them, essentially all of them. Now maybe at the end they give you some glimmer of hope, but somethin…
Why I’m Not Leaving California | Responding To Comments
What’s up you guys, it’s Graham here. So the other day I posted in the YouTube community tab, but only a few people even know it exists. In there, I asked you to post the most interesting questions that you wanted me to answer. Now, initially, I was plan…
Why You've Never Had an Original Thought
Picture this: you’re in a work meeting attempting to troubleshoot a problem that your team has been struggling to figure out. You suggest something—a solution equal parts ingenious and elegant. Your co-workers are impressed and shower you with praise, all…
The Nurse Keeping Explorers Alive | Podcast | Overheard at National Geographic
Foreign. This is a National Geographic map of the world. We’re in a basement office at National Geographic headquarters, and Karen Berry is standing in front of a huge map that stretches from floor to ceiling. Like a military general, she points out explo…
Why Being Busy is Ruining Your Life
Hey, it’s Joey. What could be better ideas? So, a lot of people think being busy is a good thing, as if picking up pieces of paper and putting them back down again, writing emails, and, you know, walking around checking your watch, and basically just havi…