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
Crypto Will Be The 12th Sector of The S&P! | Bitcoin 2022
[Music] It’s pretty chaotic here on the first day because nobody knows where to go. There’s 50,000 people showing. The first day probably about 250,000 by the time this is over, and it’s really going to be big this year because there’s so many institution…
Frankish women in the Carolingian Dynasty primary source | World History | Khan Academy
In this video, I want to talk about the lives of Frankish women who lived during the Carolingian Dynasty in the 8th and 9th century. So you can see here in blue the Carolingian Dynasty, and the Carolingian Dynasty ruled over much of the former territory o…
Warren Buffett's BIGGEST Investment Just Posted RECORD Results!
Uh, we would have, uh, one of the fellows in the office has about 10 million shares, and I have for Berkshire’s account about 123 million. So we got about 133 million shares—one of them bought, and then you, as a result, bought some additional. One of the…
This Rock Climbing Kid Has a Hidden Strength: His Super Mom | Short Film Showcase
The skill of just being disciplined, being able to stay on track and just fight, and even take a few knocks and get back up, and just keep, you know, on that path or whatever you choose in life, that’s a skill I think that’ll be with him forever. I think …
Relative pronouns | The parts of speech | Grammar | Khan Academy
Grammarians, we’re going to talk about relative pronouns today. What relative pronouns do is they link clauses together, specifically independent and dependent clauses. If you don’t know what independent and dependent clauses are, that’s okay. Just suffi…
How to Build Products Users Love with Kevin Hale (How to Start a Startup 2014: Lecture 7)
All right, so um when I talk about making products users love, um what I mean specifically is like how do we make things that has a passionate user base that um our users are unconditionally wanting it to be successful both on the products that we build b…