Java Tutorial for Beginners #3 - Basic Operators

Tech With Tim · Beginner ·🛠️ AI Tools & Apps ·7y ago

Key Takeaways

This Java tutorial for beginners covers basic operators, variables, and data types, including the use of double data type for decimal values and the order of operations in Java, with demonstrations of code using an IDE and the Math.pow() function.

Full Transcript

hello and welcome to the third video in my jabba programming series now in this video I'm gonna be adding on to some of the stuff I did with variables in the last video and I am going to be talking about basic operations like plus minus subtraction division exponential all of that fun stuff so without further ado let's get started now when I first gonna do is just talk about another way that we can create variables in Java so earlier you saw me do something like this in 2x equals 6 ok this is fine this works we already know this but there's another way that we can actually create this variable and to do that we can actually omit this equals 6 so if we do this we just say int X what happens now is we've declared that X is a variable it exists but we have not given it a value so we've declared it but we have not initialized it I'll show you what I mean by this by just doing system.out.println and we're simply going to print X and just see what we get oh probably help I spelled print correctly okay wow I really messed that one up print LM ok run this and you can see we are already warned that there is an error so we'll proceed but and it says the local variable X may not have been initialized so before we can actually use the variable X when we set up like this we have to initialize it now to do that any line underneath this declaration we can just say X is equal to and then whatever value you want it to be as long as it's an integer value so in this case I'm going to say X equals 6 and now if we run the program everything works fine and we get 6 now obviously we can do this with every datatype so if I do like char and char X right and that's fine I say char x equals single quotation marks F like that okay then we can print that out and that will work fine now what I want to talk about now is operations so I am going to create a few variables I'm going to say integer x equals 5 don't forget your semicolon integer y equals 7 and I'll say int Z is equal to 56 ok and I'll create one more variable and swear we're gonna start talking so what I want to do now is I want to sum all of these variables so 56 7 and 5 but I don't want to write like 56 plus 7 plus 5 okay because yeah that's the value of these variables this works fine but what if I were to change this variable Y well that means I have to go down here and I have to change this as well to whatever I change Y to what if I want to change x and y well that means I have to change both of these so there's a really cool thing that we can do and we can just use the variable names and just add them up so we can say X plus y plus Z like this okay and now our variable sum stores the value of the sum of these three variables and to prove it to you I will print it to the screen and you can see that we get 68 as our sum now furthermore if we wanted to take the difference of all these and subtract them all we have to do is simply replace this with a minus sign and we get negative 58 okay now to multiply things this is another operator we can do multiplication like this and replace this one with multiplication we'll multiply all these out together you can see we get 1960 as our value to divide we can use the forward slash now I'm not gonna do that right now because that is kind of a different topic like there's a weird thing with division I have to talk about but that's how you do that so there are four basic operators now these work just like they work in math in terms of order of operations right so we're gonna start off with exponents and then go bracket our brackets exponents division multiplication and so on through the process okay I assume you guys know order of operations something's if I do something like x times y minus said what's first gonna operate is X multiplied by Y and then we are going to subtract said from whatever this value is now again if I switch this on you're like y x said what's gonna happen first is y x z and then the subtraction is going to happen now for example if we have the operators that have the same presidents or the same I don't know like order of operation level I don't know what to call it it's just going to order operate from left to right so it's gonna do X multiplied by Y multiplied by Z so this is a common occurrence allowed people to understand this if we do a Division sign we're first gonna multiply x times y and then we're gonna divide by Z afterwards okay so that's how that works now one more whenever we're doing operations like this we can also add brackets in so if I wanted to for example say X multiplied by Y and then divided by Z I could do something like this by throwing brackets in here so now it's showing a hundred percent that's we are going to multiply this before we divide and same thing if I did this now whatever's in the brackets is gonna happen first and then I can divide by Sud and obviously we can embed more brackets in here so I can say like x y times y okay like get out as many brackets and layers as you want and that's totally acceptable and that totally works okay so now we are going to talk about division more specifically and then go into a few more operators because division works a little bit different in Java is it just in general okay so if I want to divide 56 by 7 so I want to divide Z by Y okay and I sum just do you okay we're gonna call this you and I print this out to the screen 56 divided by 7 gives me a value of 8 that is because our data type for the variable U which is holding the value of Z and Y or does that divided by Y is an integer so it actually isn't able to give us a floating-point number although we know that this number is a floating-point number is it let's see let's make sure this is not messed up 57 divided by this guy yeah so it can't give us a floating-point number because this is well and int data type so it assumes we want an integer in return so therefore just gonna give us the value and terminate all the decimals okay so say this numbers like eight point something just gives us eight right removes the remainder now if I want to get decimal points what you will want to do most the time I could try to do something like this okay double of U equals Z divided by Y and you mean okay that makes sense double like it's gonna give us the floating point but watch what happens if I run this we just get a point oh well we know that 57 divided by seven is not 8.0 it has some decimal component to it that I couldn't tell you right now but why aren't we getting that well that is because the two data types that we are dividing are both integers which means when we get a value back from this it's going to be an integer value and then all we do is convert it into double because we have this double here okay just by adding that dot zero so how can we ensuring we get a floating point well there's two things we could do we could first change the bottom to be a double and we could change the top to be a double so I could do like this okay it's like double and could change this to be a double all right and if we have both of these double and we try this now I can see that we get our decimal point okay and it shows up and gives us like whatever that is now what if I just make one of these so I say int Y and this is double ID let's try this now you can see we still get our floating point that is because if one of the values that we're dividing here is a double it's going to automatically make the whole thing a double so one of them is a double everything becomes a double and this is the way it works for all the operations so if you have W and in this case we say like x times y or let's say x times Z since Z is a double X is not we're still gonna get a double value so if we run this you can see that we get this little point O so if one of our operands is a double then that means everything is going to be a double when we use it or whatever okay now exponent the way that we can do exponent is we have to bring in module I believe but it's like math dot POW and then in here you put the what he called the base and the exponent there's not a star star like in most languages guy let me say int of D is equal to math POW and here I'm simply going to raise X to the power of Y all right so we get math up POW let's see what is this saying convert okay so what's this int int int hmm cannot convert form double to form int interesting why it's telling me that let's just try this maybe there we go okay so whenever we get x x exponents apparently they have to be in the form double okay didn't know that but all right so now if i want to print out d what's going to happen is we get the exponent for this okay so math dot pow this is your base and this is what you're raising the exponent to so 5 to the 7 apparently is equal to that okay so that is pretty much it for operators i guess obviously you can have as many operators in one line as you want you now understand how things work in terms of doubles and int so if one of the operators in the whole chain of operators that you're opera seria Buhl's that you're adding subtracting dividing whatever is a double then that means you're going to get a double value back for 100% if all of them are integers that means you're going to get an integer value back now same thing here so i have let's change Y back to double and so let's change u to be int okay so it says in to you x time set so now that's x times y okay so now you can see we're getting an error here cannot convert from double to int and that is because we're trying to say that the integer u is equal to x times y but why is a double value so when we get a value back here it's gonna be double so we can't convert that into an integer just by doing this and there's another way that we can do it that I'm going to show you in a second okay so we would have to make sure that this stays us double and it's nice in this IDE it tells you when you made a mistake like that because a lot of times you might not really see that in your program okay now I'm quickly gonna go over something called typecasting we're going to talk about this a lot more later but I'm just gonna show you like fairly quickly how this works I'm just gonna delete this line and we're going to turn these back into integers okay so if I want to do something like X divided by Y okay and I want to make sure that I'm getting that value the decimal point value okay like so a double rather than converting these like the Declaration of our variable to a double something we can do called typecasting into typecast we are changing in line without changing the declaration the type of the variable the way to do this is to simply put in brackets the type that you want to convert your variable into and then directly afterwards is the variable you want to convert so in this case it's double and then we have Y okay so if I run this now and I print you instead of D you can see that we get the decimal value that we were looking for if I remove this double right then we do not get that or we do just cuz it says double but you guys see the point okay so anyways I think that is gonna be it for this video in the next video what are we gonna be covering got a look at my guide here we're gonna be talking about input and output so how can actually get input from the user in the console and then doing things based on that input printing them to the screen adding subtracting whatever okay so if you guys enjoyed the video as always please make sure you leave a like and I will see you again in the next one [Music]

Original Description

Java beginners programming tutorial 2018. A complete java tutorial meant for absolute beginners. Absolutely no programming experience required. If you are new to programming and want to get started you are in the right place! In this specific video I will be showing how to use the basic operators in Java. Such as + , - , / , * . ************************************************************** One-Time Donations: https://goo.gl/pbCE9J Support the Channel: https://www.patreon.com/techwithtim Join my discord server: https://discord.gg/pr2k55t ************************************************************** Please leave a LIKE and SUBSCRIBE for more content! Tags: - Tech With Tim - Java Tutorials for Beginners - Java operators - Java beginner tutorial - Beginner Java tutorial
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Tech With Tim · Tech With Tim · 0 of 60

← Previous Next →
1 A* Path Finding Algorithm(Visualization)
A* Path Finding Algorithm(Visualization)
Tech With Tim
2 Python Programming Tutorial #1 - Variables and Data Types
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
3 Python Programming Tutorial #2 - Basic Operators and Input
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
4 Python Programming Tutorial #3 - Conditions
Python Programming Tutorial #3 - Conditions
Tech With Tim
5 Python Programming Tutorial #4 - IF/ELIF/ELSE
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
6 Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
7 Python Programming Tutorial #6 - For Loops
Python Programming Tutorial #6 - For Loops
Tech With Tim
8 Python Programming Tutorial #7 - While Loops
Python Programming Tutorial #7 - While Loops
Tech With Tim
9 Python Programming Tutorial #8 - Lists and Tuples
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
10 Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
11 Python Programming Tutorial #10 - String Methods
Python Programming Tutorial #10 - String Methods
Tech With Tim
12 How to Overclock a NVIDIA GPU
How to Overclock a NVIDIA GPU
Tech With Tim
13 Python Programming Tutorial #11 - Slice Operator
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
14 Python Programming Tutorial #12 - Functions
Python Programming Tutorial #12 - Functions
Tech With Tim
15 Python Programming Tutorial #13 - How to Read a Text File
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
16 Python Programming Tutorial #14 - Writing to a Text File
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
17 Python Programming Tutorial #15 - Using .count() and .find()
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
18 Python Programming Tutorial #16 - Introduction to Modular Programming
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
19 Python Programming Tutorial #17 - Optional Parameters
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
20 Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
21 Python Programming Tutorial #19 - Global vs Local Variables
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
22 Python Programming Tutorial #20 - Classes and Objects
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
23 Cool VBS Script to Prank Your Friends!
Cool VBS Script to Prank Your Friends!
Tech With Tim
24 How to Overclock an AMD GPU
How to Overclock an AMD GPU
Tech With Tim
25 Best GPU'S For Mining Ethereum (2018)
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
26 Recursion and Memoization Tutorial Python
Recursion and Memoization Tutorial Python
Tech With Tim
27 Ethereum Mining Rig - Hardware Guide
Ethereum Mining Rig - Hardware Guide
Tech With Tim
28 Pygame Tutorial #1 - Basic Movement and Key Presses
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
29 How to Install Pygame (Windows 8/10)
How to Install Pygame (Windows 8/10)
Tech With Tim
30 How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
31 How to Mine Ethereum 2018 - WORKING (Super-Easy)
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
32 Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
33 Pygame Tutorial #2 - Jumping and Boundaries
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
34 Pygame Tutorial #3 - Character Animation & Sprites
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
35 Pygame Tutorial #4 - Optimization & OOP
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
36 OBS Studio Tutorial - Best OBS Settings
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
37 Linear Search Algorithm - Python Example and Code
Linear Search Algorithm - Python Example and Code
Tech With Tim
38 Make Any Mic Sound AMAZING! (WITH OBS)
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
39 Binary Search Algorithm - Python Example & Code
Binary Search Algorithm - Python Example & Code
Tech With Tim
40 Pygame Tutorial #5 - Projectiles
Pygame Tutorial #5 - Projectiles
Tech With Tim
41 Pygame Game - Mini Golf
Pygame Game - Mini Golf
Tech With Tim
42 Pygame Tutorial - Projectile Motion (Part 1)
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
43 Pygame Tutorial - Projectile Motion (Part 2)
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
44 Pygame Tutorial #6 - Enemies
Pygame Tutorial #6 - Enemies
Tech With Tim
45 Pygame Tutorial #7 - Collision and Hit Boxes
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
46 Pygame Tutorial #8 - Scoring and Health Bars
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
47 Cloud Mining vs. Hardware Mining - 2018
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
48 How to Install Pygame on Mac OSX (Fast-Simple)
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
49 Pygame Tutorial #9 - Sound Effects, Music & More Collision
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
50 Pygame Tutorial #10 - Finishing Touches & Next Steps
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
51 How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
52 How to Create a Button in Pygame [CODE IN DESCRIPTION]
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
53 Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
54 Pygame Side-Scroller Tutorial #2 - Random Object Generation
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
55 Pygame Side-Scroller Tutorial #3 - Collision
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
56 Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
57 How to Create A Message Box in Python - Tkinter
How to Create A Message Box in Python - Tkinter
Tech With Tim
58 Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
59 How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
60 Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim

This Java tutorial teaches beginners how to use basic operators, variables, and data types, with a focus on the double data type and order of operations. Viewers will learn how to write Java code using these concepts and understand how to apply them in practice.

Key Takeaways
  1. Declare variables without initialization
  2. Initialize variables before use
  3. Use basic operators in expressions
  4. Replace operators with variable names in expressions
  5. Run the code with int and double variables
  6. Try to convert double to int using typecasting
💡 The order of operations in Java follows the standard mathematical order of operations, and using the double data type allows for decimal values, which is important for accurate calculations.

Related AI Lessons

Up next
I Asked ChatGPT to Apply to 500 Jobs (8 Interviews in 48 Hours)
Sabrina Ramonov 🍄
Watch →