Java Full Course for free ☕
Ringkasan
TLDRThe video is a comprehensive guide on getting started with Java programming for beginners. It begins with an overview of why learning Java is beneficial, emphasizing its popularity, flexibility, and strong job prospects. The instructor explains the difference between high-level and low-level programming languages and the process of compiling Java source code into bytecode, which is then executed with the help of a Java Virtual Machine (JVM). Viewers are guided through the steps to download and install the Java Development Kit (JDK), which includes necessary development tools and the JVM. Additionally, the importance of an Integrated Development Environment (IDE) is discussed, with recommendations to use Eclipse or IntelliJ IDEA for ease of writing and testing code. The tutorial covers basic concepts, including setting up a Java project in an IDE, creating a class and a main method, using print statements to display output, and compiling and running Java programs. The video also provides practical tips like changing color themes and shortcuts in IDEs and concludes with some recommendations on other useful tools and future learning pathways in Java.
Takeaways
- 📚 Understand the basics of Java programming.
- 💼 Java's relevance in the tech industry and job market.
- 🔧 How to install JDK and set up an IDE.
- 💻 Compiling Java code into bytecode.
- 🔀 Explanation of the cross-platform capabilities of Java.
- 🧩 Using IDEs for Java development.
- 🖋️ Creating and running a simple Java program.
- 🎨 Tips for customizing IDE settings.
- ➡️ Importance of bytecode and JVM.
- 🚀 Beginners' roadmap to learning Java.
Garis waktu
- 00:00:00 - 00:05:00
Introduction to a beginner-friendly Java tutorial by the host, emphasizing the importance of learning Java due to its flexibility and job opportunities. The host provides an overview of computer languages, explaining the difference between high-level and low-level languages, and introduces Java's bytecode, which allows cross-platform compatibility using JVM and JDK.
- 00:05:00 - 00:10:00
Instructions on downloading Java JDK from the official Oracle website, followed by the installation process. The host then introduces IDEs, specifically recommending Eclipse or IntelliJ IDEA, to begin coding with Java, highlighting the convenience and benefits of using an IDE over basic text editors.
- 00:10:00 - 00:15:00
Steps on setting up Eclipse IDE, including downloading, extracting, and launching the program. The host explains the process of creating a new Java project and setting up the JRE within Eclipse, preparing for the development of a basic Java application.
- 00:15:00 - 00:20:00
Introduction to creating a simple Java application within Eclipse by setting up a class and a main method. The host explains the importance of the main method as the entry point of execution in a Java program and demonstrates how to print text using System.out.print/println, including an explanation of escape sequences for formatting.
- 00:20:00 - 00:25:00
A detailed explanation of comment usage in Java for documenting and annotating code, distinguishing between single-line and multi-line comments. The host offers tips on changing Eclipse IDE's appearance for better visual comfort and introduces shortcuts and tricks to improve coding efficiency.
- 00:25:00 - 00:30:00
Introduction to variables in Java, explaining the concept and purpose of variables, the types of data they can hold, and the basic data types available in Java. The host also explains the differences between primitive and reference data types, illustrating with examples.
- 00:30:00 - 00:35:00
Practical demonstration of declaring, initializing, and using variables in Java. The host explores various data types (int, long, float, double, boolean, char, String) with detailed examples, including common conventions and best practices for each.
- 00:35:00 - 00:40:00
The process of swapping the values of two variables using a temporary variable. The host demonstrates a classic programming exercise and explains the importance of using a temporary variable when no direct function to swap variables exists.
- 00:40:00 - 00:45:00
Introduction to Java's Scanner class for accepting user input, covering how to create and use a Scanner object, and demonstrating handling different types of input (String, int). The host warns about common pitfalls and solutions when mixing input types, specifically after using nextInt before nextLine.
- 00:45:00 - 00:50:00
Explanation of arithmetic expressions in Java, distinguishing between operands, operators, and explaining the use of basic arithmetic operations. The host introduces the concept of integer division and its implications, presenting solutions for maintaining decimal precision.
- 00:50:00 - 00:55:00
Guide on creating basic GUI applications in Java using JOptionPane. The host demonstrates how to create input and message dialog boxes for interacting with users, with additional guidance on converting data input to appropriate data types.
- 00:55:00 - 01:00:00
Overview of Java's Math class and its utility methods, such as max, min, abs, sqrt, round, ceil, and floor. The host provides a practical project to calculate the hypotenuse of a triangle using Math methods, encouraging the use of these methods in real-world applications.
- 01:00:00 - 12:00:00
Tutorial on generating random values in Java using the Random class. The host explains the instantiation and usage of the Random class to generate random integers, doubles, and booleans, highlighting the importance of random number generation in fields such as game design.
Peta Pikiran
Pertanyaan yang Sering Diajukan
What is the main focus of this video?
The video teaches beginners everything they need to know to get started with Java.
What are the reasons to learn Java according to the video?
Java is one of the top three most popular programming languages, it's flexible, widely used by businesses, and can lead to a well-paying job.
What does compiling Java source code produce?
Compiling Java source code produces bytecode, which is cross-platform and has a .class file extension.
What is a JDK?
JDK stands for Java Development Kit and it contains developers' tools, a Java Runtime Environment (JRE), and a Java Virtual Machine (JVM).
What should be downloaded to get started with Java programming?
You should download a JDK (Java Development Kit) and optionally an IDE like Eclipse or IntelliJ IDEA.
Why is Java considered cross-platform?
Java's bytecode can run on any platform with a JVM, making it cross-platform.
What is bytecode in Java?
Bytecode is a compiled format of Java source code that is platform-independent and can be executed on any operating system using a JVM.
What is the purpose of an IDE?
An IDE, or Integrated Development Environment, helps write, check for errors, compile, and run code efficiently.
What is the first example program created in the video?
The video walks through creating a simple Java program including downloading necessary tools and writing and running a basic Java class.
Why do beginners use print statements?
Print statements are used to display output to the console, which is useful for learning and debugging.
Lihat lebih banyak ringkasan video
(Part 1) The Trek: A Migrant Trail to America | The Whole Story with Anderson Cooper
Theory of Architecture | #1 - Timothy Brittain-Catlin
Abaqus Truss Analysis Lecture 1 | 2D Three Member Plane Truss | Deflections and Support Reactions
How to Unhide Apps on iPhone (iOS 18)?
अनजान लोगों को कैसे लाऐँ बिज़नेस में | Prospect On Purpose | Network Marketing Training | APratihast
From Skydiving to Multimillionaire: @manikaggarwalofficial 's Inspirational Story #multimillionaire
- 00:00:00how's it going everybody it's bro hope you're doing well
- 00:00:03and in this video i'm going to teach you guys everything you need to know to get
- 00:00:06started with java so sit back relax and enjoy the show
- 00:00:10if you wouldn't mind please like comment and subscribe
- 00:00:16one like equals one prayer for the youtube algorithm
- 00:00:20here's an outline of the topics covered in this video if you would like to skip
- 00:00:24ahead to a certain section feel free to click on any of the
- 00:00:27timestamps posted in the description also at the end of this video we're
- 00:00:30going to be discussing some tips and tricks
- 00:00:33so be sure to watch until the very end i'll give you three reasons why you need
- 00:00:37to learn java besides being one of the top three most popular programming
- 00:00:41languages worldwide java is an extremely flexible language
- 00:00:45it's used extensively by business enterprises android apps games
- 00:00:49and if you learn java you could land a job as a java developer
- 00:00:53according to glassdoor entry-level java developers have an average starting
- 00:00:57salary of 70 000 that's nothing to sneeze at
- 00:01:01so why not learn java are you still here okay cool
- 00:01:04let's begin with the basics computer languages are on a spectrum between
- 00:01:08being high level and low level computers only understand
- 00:01:12binary it's referred to as machine code it's a low level format that a machine
- 00:01:17can understand however humans have difficulty reading
- 00:01:20binary since it's all ones and zeros to create machine code we write in a
- 00:01:24format called source code which is understandable by humans and
- 00:01:28compile to machine code when we create
- 00:01:31javasource code the file ends with a dot java file extension think of
- 00:01:36compiling code as transforming source code to machine code
- 00:01:40we do this because machines can't read source code and vice versa
- 00:01:44humans have trouble reading machine code unless you're a robot or an android or
- 00:01:47something however when we compile our source code
- 00:01:51to machine code it's machine specific if we write source code
- 00:01:54and compile on a mac we can only run that code on a mac and
- 00:01:59the same concept applies for pcs although the java
- 00:02:03language has a solution for this problem with java we have an intermediary step
- 00:02:08where we can compile our source code to a format called
- 00:02:11bytecode bytecode is cross platform and ends with a
- 00:02:15dot class file extension here's an example of java source code
- 00:02:19and here's an example of that same source code after we compile it to
- 00:02:23bytecode it's kind of funky right since bytecode
- 00:02:25is cross-platform you could write your code on a mac
- 00:02:28and then send your bytecode file to your friend who can then run it on their pc
- 00:02:33using a jvm to translate the bytecode to machine code but we are going to need
- 00:02:38the help of a jvm to translate bytecode to machine code but where can we get a
- 00:02:43jvm well it's included with a jdk and what is a jdk well
- 00:02:48jdk is an acronym for java development kit
- 00:02:52it contains developers tools to help us code as well as a
- 00:02:55jre a java runtime environment which contains a library
- 00:03:00toolkits and our jvm which is another acronym
- 00:03:04for java virtual machine which translates by code for us
- 00:03:08to machine code so all you need to worry about
- 00:03:11is downloading a jdk and everything else will be included
- 00:03:14and now that we know what a jdk is it's time to download
- 00:03:18one so open up the internet and go to any search engine
- 00:03:21and look this up java jdk download go to the first link java se downloads
- 00:03:27sc stands for standard edition go to
- 00:03:32jdk download scroll down and find the appropriate file
- 00:03:36for your operating system since i'm running windows i'm going to download
- 00:03:40this exe version
- 00:03:43agree to whatever and download and when this finishes downloading i'm
- 00:03:50going to open this open when done on my computer i
- 00:03:53currently have a jdk already installed but i'm going to go
- 00:03:56ahead and reinstall it for the sake of this video click
- 00:04:00next next then wait a little bit and close i would also recommend
- 00:04:09an ide that's another acronym and it stands for
- 00:04:12integrated development environment think of it as software that helps us write
- 00:04:17other software you could write code with a text editor
- 00:04:20such as notepad and then compile the text file but doing
- 00:04:24so is not really beginner friendly so an ide provides an interface for us
- 00:04:29to write code check for errors compile and run code
- 00:04:33there's two ides that i would recommend they are both eclipse or intellij idea
- 00:04:38it doesn't matter which one you download because the code that we write is still
- 00:04:42the same so let's download an ide now it's time
- 00:04:45to install the ide i would recommend either the eclipse
- 00:04:49ide or intellij idea i'm more comfortable
- 00:04:52with eclipse so i'm going to stick with the clips
- 00:04:55so go back to the interwebs and look up either
- 00:04:58eclipse ide or intellij idea ide so i'm going to look up the clips click
- 00:05:04the first link click this orange download button go to
- 00:05:08download packages and select eclipse ide for java developers
- 00:05:13and select the correct download for your machine i'm going to select the download
- 00:05:18for windows and click download and then just wait a
- 00:05:23little bit again like usual for me this is currently a zip file so i
- 00:05:27need to select this file and extract all
- 00:05:34with the newly extracted folder navigate to this eclipse application
- 00:05:38so you can select this to launch eclipse for convenience i'm going to create a
- 00:05:43desktop shortcut so for me i'm going to go to where is it
- 00:05:47send to create desktop shortcut and then click to launch
- 00:05:56you can select a workspace i'm going to use the default and
- 00:05:59click launch
- 00:06:03we are now within eclipse and we can begin a new project
- 00:06:06we are now ready to rock and roll so let's begin by creating our first
- 00:06:10java program but in order to do so we need to create a
- 00:06:14java project if you're brought to this welcome screen
- 00:06:17you can close out of this because it's annoying and in order to create a java
- 00:06:21project navigate to your package explorer and
- 00:06:24select create a java project if you're missing the package explorer
- 00:06:28you can go to file new dropper project and that will take
- 00:06:32you to the same place we need a unique name for this java
- 00:06:35project i will call this my first program
- 00:06:42and i will want to configure the jre the java runtime environment and we
- 00:06:47downloaded that with the jdk because the jre is a
- 00:06:52component of the jdk so i'm currently using 13.
- 00:06:56i'm going to change this to 15.
- 00:06:58that was the one that i more recently
- 00:07:00downloaded just now so go to configure jres and i'm going to click add
- 00:07:07select standard vm vm is virtual machine click next go to jre home
- 00:07:15go to directory and i'm going to make sure that i'm selecting the most
- 00:07:20recent jdk for me that is 15 select folder finish apply
- 00:07:27apply and close then finish if this window pops up you can select
- 00:07:33don't create that's to create a module if you look to the left hand side within
- 00:07:36the projects folder we now have a java project called
- 00:07:40my first program but we will need to add what is called
- 00:07:43a class to this project a class is a collection
- 00:07:47of related code so in order to add a class to this project i'm going to
- 00:07:51select this project folder then go to file new class
- 00:07:57and we need a unique name for this class i usually call this main but you can
- 00:08:02name it whatever you want and then we are going to check this
- 00:08:06public static void main checkbox and then click finish
- 00:08:11with that out of the way take a look back within your project folder and you
- 00:08:14should now have a java file that shares the same name as
- 00:08:18your class name my class name is named main therefore
- 00:08:22my java file is also called main so this has the dot java file extension
- 00:08:29and with what we discussed before this is source code
- 00:08:32it's in a format that humans can easily read and understand
- 00:08:36and when we compile this source code to bytecode
- 00:08:40we're going to create a new file that has the
- 00:08:43dot class file extension and with that bytecode file
- 00:08:47we can run that and translate it using a jvm
- 00:08:50a java virtual machine here's our java file and we have
- 00:08:54our class and mine is called main so all the class
- 00:08:57is is that it's a collection of related code
- 00:09:00we won't be exploring in depth on the topic of classes until we reach the
- 00:09:03subject of object-oriented programming which is
- 00:09:07about 20 videos into this playlist so you have some time so this is our
- 00:09:11class mine is called main anything within the outer set of curly braces
- 00:09:15belongs to the class and is contained within
- 00:09:19and within our class we have what is called a main
- 00:09:22method our program won't run without this method because when we run our code
- 00:09:27we begin by calling the main method so if we were to compile and run this code
- 00:09:32you can do so by clicking the screenplay button
- 00:09:35all output is displayed to the console window and nothing appears to happen
- 00:09:39because we haven't written anything yet so if we were to remove this main method
- 00:09:44and tried to do this again we would encounter an error because our
- 00:09:49main method was not found in the class main it's asking us
- 00:09:53to please define the main method now looking back when we
- 00:09:57created our class we went to file new class and in order to generate
- 00:10:02the main method we checked this checkbox here
- 00:10:05that states public static void main so the main method generated for us when
- 00:10:11we created this class but if we're missing it we
- 00:10:14can easily just type it in a textbook that i read in college said
- 00:10:17to think of the main method as a magical spell or incantation that
- 00:10:21we have to say in order to get this program to run so
- 00:10:25we are currently missing a main method but we can easily just type it in
- 00:10:28so repeat after me public static void main then we need some parentheses
- 00:10:35string straight braces args and then a set of curly braces and that is it we
- 00:10:41now have a main method and our program runs and compiles just
- 00:10:46fine so any code within the main method will
- 00:10:50execute starting at the top and then work its way down so with the
- 00:10:54main method any code you place at the top will be
- 00:10:57executed first so let's print something to the console
- 00:11:00window in order to display some text all you have to do is type this system
- 00:11:05with a capital s dot out dot print then you need a set of
- 00:11:12parentheses and then a semicolon at the end so
- 00:11:16within the parentheses of this print method we can type some
- 00:11:20text to display to the console window but we need to make sure that our text
- 00:11:25is within a set of double quotes and we can display some text let's say
- 00:11:30i don't know what's a food you like i love
- 00:11:34pizza so if i were to run and compile this
- 00:11:37it's now going to print i love pizza to the console window
- 00:11:41let's say that we would like to display another line of text
- 00:11:44directly underneath the first we can accomplish that by using
- 00:11:47another print statement so for convenience i'm going to copy this first
- 00:11:52line paste it directly underneath and display
- 00:11:55some other text such as it's really good
- 00:12:00so when i compile and run this pay attention the output is
- 00:12:04one long line of text the reason that this is all displaying as
- 00:12:08one long line of text is because after printing the first statement
- 00:12:13our cursor does not move down to the next line
- 00:12:16in order to do so we could use a print ln statement
- 00:12:20short for print line it's as if we're hitting
- 00:12:23enter when we finish outputting our text so let's try this again
- 00:12:27using a print ln statement and now each line of text is on its own
- 00:12:34individual line so that's what distinguishes a print and
- 00:12:38print line statement a print line will add a new line character as if you're
- 00:12:42hitting enter when you finish outputting your text
- 00:12:44whereas a print statement does not so that's the difference between the two
- 00:12:49an alternative to using a print line statement is that we could stick with
- 00:12:53the standard print statement and at the end of our text add what is referred to
- 00:12:56as an escape sequence for a new character
- 00:13:00now an escape sequence is a character preceded with
- 00:13:03a backslash and one of a few characters that follows directly afterwards
- 00:13:07this is an escape sequence for a new line character
- 00:13:11when we add this escape sequence for a new line it's as if we're hitting enter
- 00:13:15wherever we place this escape sequence so within our
- 00:13:18string of text for our first line at the end we're
- 00:13:20going to add backslash n and this will have the same
- 00:13:24effect as a print line statement it's going to
- 00:13:27display our text and then move the cursor down to the
- 00:13:29next line as you can see there is no additional change to the output within
- 00:13:33the console window now what if we reverted our print
- 00:13:36statements back to print line statements and kept the additional escape sequence
- 00:13:40in for a new line character well we're going to have an extra empty
- 00:13:45line of text because we're displaying our line of output plus an additional
- 00:13:49character for a new line and then we're hitting enter at
- 00:13:52the end via the print line statement so we're going
- 00:13:55to have an additional empty line between these two lines of text if we were to do
- 00:13:59that so a few other escape sequences that you
- 00:14:02might be interested in include the following a backslash t will
- 00:14:06add a tab so let's precede our text with an escape
- 00:14:09sequence for a tab which is backslash t
- 00:14:14so this is if we're hitting tab before displaying our text
- 00:14:17and we now have some empty space preceding our line of output
- 00:14:21if you need to put something within quotes let's try to do so normally
- 00:14:25so our compiler is actually going to be confused because we cannot normally add
- 00:14:30a set of quotes because our text already needs to be surrounded with quotes
- 00:14:35so if we need to literally display some quotes
- 00:14:38some double quotes we need to precede our double quotes with
- 00:14:42an escape sequence so backslash then quotes so this will allow us to
- 00:14:47literally print some double quotes so we're going
- 00:14:51to surround our first line of text with some double quotes now and if you
- 00:14:55need to display a backslash then you need to use
- 00:15:00double backslashes because if you use just one your compiler thinks you're
- 00:15:04trying to use an escape sequence and that's how to display a backslash in
- 00:15:08summary anything preceding with a backslash is the beginning of an escape
- 00:15:13sequence and there's one of a multitude of characters
- 00:15:15that could follow afterwards and depending on the character
- 00:15:19this has special meaning for your compiler to do something specific
- 00:15:23now anything that is following two forward slashes
- 00:15:26is the beginning of a single line comment i could write
- 00:15:30this is a comment and this line of text is going to be ignored by the compiler
- 00:15:36so there's going to be no change to this program with the
- 00:15:39additional comment anything that is a comment is ignored by
- 00:15:42the compiler so it's useful if you need to leave yourself a note
- 00:15:45or for somebody else that's looking over your code if you need a multi-line
- 00:15:49comment that is a forward slash followed by an
- 00:15:52asterisk and anything up to an asterisk and
- 00:15:57another forward slash will be the bounds of this comment so i
- 00:16:00could write on a new line for each word this is a
- 00:16:05comment and all of this will also be ignored by the compiler
- 00:16:09so that's how to write a multi-line comment a forward slash and an asterisk
- 00:16:15and anything up to another asterisk and forward slash
- 00:16:19so those are comments alright ladies and gentlemen it's time for this section on
- 00:16:23tips and tricks and for my first trick i'm going to change the color scheme of
- 00:16:27my ide we're currently using the light theme
- 00:16:30but i much prefer the dark theme i'm going to be joining the dark side so
- 00:16:33in order to change the color scheme of your ide go to if
- 00:16:37you're using eclipse window preferences under the general tab
- 00:16:42go to appearance theme and you can change the theme here
- 00:16:47i will click dark i'm going to select apply
- 00:16:51okay and then apply and close so the dark theme is great if you want to feel
- 00:16:55like a pretend elite hacker for my next trick i'm going to change
- 00:16:59the font color as well as the background color of my console window in order to
- 00:17:03do so head back to window preferences
- 00:17:07under run debug go to console and you can change the color schemes here
- 00:17:12i'm going to change the text color to a bright green
- 00:17:15click ok as well as the background color to
- 00:17:18a slightly lighter shade of black that should be good when you're finished
- 00:17:24click apply and then apply and close and you may
- 00:17:26need to run this again to see the changes
- 00:17:28so that's how to change the font color as well as the background
- 00:17:32color of your console window so it's somewhat tedious to have to write a
- 00:17:36print line statement correct system dot out dot print line normally
- 00:17:40that's a lot to type so a shortcut would be to type sys out
- 00:17:44then hold control space and your ide will auto generate the rest of this
- 00:17:49print line statement for you let's move on to trick number four let's
- 00:17:53say that we have hundreds of different print line
- 00:17:56statements and we need to change the text
- 00:17:58to print because we made a mistake so there's a feature
- 00:18:02where we can replace some text in your program with another
- 00:18:06so let's pretend we would like to replace print line with print
- 00:18:10so go to edit find replace and we can replace
- 00:18:13some text with something else let's find each instance of print line
- 00:18:18and replace this with print then click replace all so that will take care of
- 00:18:24all that for you let's move on to some final tips so with
- 00:18:27spaces spaces don't make much of a difference within your code for example
- 00:18:32after this dot and my print portion of this print statement i could add a
- 00:18:37bunch of spaces for no reason and this would run and
- 00:18:40compile just fine i'm not sure why you would do that but
- 00:18:44that's just to reinforce the point that spaces don't make much of a difference
- 00:18:48unless you're using a space to split up some keywords
- 00:18:51then you might run into an issue or if you're adding space to a string
- 00:18:55well then that's going to have a noticeable effect so spaces
- 00:18:58for the most part don't really matter too much depending on where they are
- 00:19:02here's a trick on zooming in or out hold control minus to zoom out or
- 00:19:07control plus to zoom in or you could go to window
- 00:19:10editor then zoom in or zoom out within this
- 00:19:13menu here's my last tip for you let's say you
- 00:19:16accidentally close out of your package explorer
- 00:19:19or your console window you can easily bring those back by going to
- 00:19:23window show view and then they are all listed here
- 00:19:26so i would like to bring back my package explorer as well as my console window
- 00:19:31alright guys and gals you should be ready to get started with java be sure
- 00:19:35to check out the full 100 video playlist as well
- 00:19:38and if you could do me a small tiny favor i would
- 00:19:41greatly appreciate it if you could help me defeat the youtube algorithm by
- 00:19:45smashing the like button drop a comment down below
- 00:19:49and subscribe if you'd like to become a fellow bro
- 00:19:51hey you yeah i'm talking to you if you learn something new
- 00:19:56then you can help me help you in three easy steps
- 00:20:00by smashing that like button drop a comment down below
- 00:20:04and subscribe if you'd like to become a fellow bro
- 00:20:06[Music]
- 00:20:15hey how's it going everybody it's your bro hope you're doing well and in this
- 00:20:30video i'm going to teach you guys all about variables in java so
- 00:20:33sit back relax and enjoy the show
- 00:20:38if you find this video helpful please remember to like
- 00:20:41comment and subscribe your support will help keep this channel running
- 00:20:46all right guys and gals let's talk about variables
- 00:20:49now a variable is a placeholder for a value
- 00:20:54and it behaves as the value that it contains do you remember from either
- 00:20:58elementary school or middle school when we were working with
- 00:21:01algebra we usually had to solve for some sort of variable like
- 00:21:04x or y and x or y contained some sort of numeric value and for all
- 00:21:10intents and purposes this variable behaved exactly as this
- 00:21:14value well with programming we can perform
- 00:21:17something similar to that but we are not limited to just numbers
- 00:21:21we could also store words whole sentences and these things
- 00:21:25called boolean values which hold either true or
- 00:21:27false but if we're going to store a value within a variable
- 00:21:31we have to list the data type of what we're planning to store within
- 00:21:35that variable is it going to be a number a word
- 00:21:39a boolean so we need to discuss data types
- 00:21:43there are eight primitive data types and a special
- 00:21:46reference data type called a string anything that i have noted with a star
- 00:21:50is particularly important so i would pay special attention to
- 00:21:54these our first data type is boolean this has a size of one
- 00:21:59bit so it can only hold two values that being
- 00:22:02true or false if we're attempting to sign a boolean value
- 00:22:06we would type either equals true or equals false
- 00:22:10something similar would be let's say we have a light switch program
- 00:22:13well if the light switch is on we could say that the light switch has a value of
- 00:22:18true if it's off it has a value of false so this is
- 00:22:21binary that's why it only uses one bit it only needs one bit of memory
- 00:22:26to function next we have byte this isn't as
- 00:22:29important as a few others but with one byte we can hold an integer
- 00:22:33number between negative 128 to 127 because a byte only has one byte of
- 00:22:39memory a short has two bytes of memory so can
- 00:22:43hold a larger number between negative 32 000
- 00:22:46and some change to 32 000 and some change so integers
- 00:22:51integers are important these use 4 bytes of memory
- 00:22:55and they can store a number to just under 2 billion
- 00:22:58to just over 2 billion because they use 4 bytes of memory and a long
- 00:23:05they use eight bytes of memory so they can hold a very
- 00:23:08large number in fact they can hold a number between
- 00:23:12just under negative nine quintillion to just over
- 00:23:16positive nine quintillion now a float they can store a fractional number
- 00:23:22specifically up to six to seven digits what makes
- 00:23:26floats different from these data types on the top here is that
- 00:23:31bytes shorts integers and longs can only store a whole
- 00:23:34integer they cannot store this decimal portion
- 00:23:38so if you're working with a program or a variable
- 00:23:41that uses a fractional number you'll need to use either a float
- 00:23:45or a double and a double has more precision it uses eight bytes of memory
- 00:23:50and it can store a fractional number up to 15 digits
- 00:23:53so in comparison with a float this has less precision
- 00:23:57than a double and for an example i just listed a few of the digits of pi
- 00:24:02with this example we can only store six to seven digits of pi but with a double
- 00:24:06we can store up to 15.
- 00:24:08there is one strange convention with floats if you're
- 00:24:11going to assign a value to a variable that's of the float data type you need
- 00:24:15to follow the value with the letter f with double variables
- 00:24:19you actually do not need to do so so that's one major difference when
- 00:24:23assigning values between these two now let's move on to
- 00:24:27characters pronounced char for short think of charizard this
- 00:24:31uses two bytes of memory and this will store a single character
- 00:24:35letter or ascii value an example would be the letter f but a
- 00:24:40common convention with assigning values to a char variable is
- 00:24:44that you need to surround this value with a pair of single quotes
- 00:24:49and our last data type is the string data type the size really varies because
- 00:24:53these are reference data types they store a
- 00:24:57sequence of characters like a word or a sentence you could
- 00:25:01store a single character within a string but chars and strings behave differently
- 00:25:06because chars are primitive data types and
- 00:25:09strings are reference data types so let's distinguish the difference
- 00:25:13between primitive and reference data types
- 00:25:15here's a super quick description between the differences of primitive
- 00:25:19and reference data types primitive data types there are it and we
- 00:25:23just discussed them they are boolean by short integer longs
- 00:25:27all those cool things that we just discussed reference data types
- 00:25:31like strings well there's an unlimited amount because
- 00:25:34they are user defined primitives store data reference data types store an
- 00:25:40address primitives can only hold one value
- 00:25:44reference data types could hold more than one value
- 00:25:47primitives use less memory compared to reference data types
- 00:25:50which use more memory and primitive data types are faster compared to reference
- 00:25:55data types which are slower now you're probably
- 00:25:58thinking cool story bro but how do we create a variable
- 00:26:01well i'm glad you asked that question so the first process with
- 00:26:04creating a variable is that we need to declare
- 00:26:07the data type of what value that this variable is going to store
- 00:26:11so come up with a variable name like x and we will precede this variable with
- 00:26:18the data type of the value that we're planning to store within
- 00:26:21this variable and then with all statements we follow this
- 00:26:24with a semicolon at the end the next step
- 00:26:27is called assignment we will take our variable and assign it equal
- 00:26:31to some sort of value of the data type that we declared this variable to be
- 00:26:36but you could combine steps one and two together
- 00:26:40and this process is called initialization
- 00:26:43we would take the data type followed by the variable name and set it equal to
- 00:26:47some value and then add a semicolon at the end to finish the statement
- 00:26:51so you can either do this in two steps with declaration and assignment
- 00:26:56or combine them both together which is initialization
- 00:26:59how about we create a few variables does that sound good to you guys so let's
- 00:27:02begin with creating an integer variable let's say int x
- 00:27:06this step is called declaration we are declaring the data type
- 00:27:11of what value is going to be contained within this variable the second step
- 00:27:15is called assignment we can assign our variable a value
- 00:27:19let's say x equals 123 and this step is called assignment or
- 00:27:25we could combine both of these steps together and this process is called
- 00:27:30initialization intex equals 123 and this would be initialization
- 00:27:38so we can do stuff with this variable it will behave as the value that it
- 00:27:42contains we could print this to the console
- 00:27:44window so within a print or print line statement
- 00:27:47we could print the value of x so make sure you do not write this within quotes
- 00:27:53right now this will display the value that is contained within x
- 00:27:56which is 123 because this variable behaves as the value that it contains if
- 00:28:02you were to surround this with quotes what we are doing now is printing a
- 00:28:06string literal you can also print text as well as a
- 00:28:11variable together let's say we have a sentence a string
- 00:28:15literal that states my number is
- 00:28:20and then if we want to do some string concatenation with a variable
- 00:28:24we would add plus and then the variable name make sure this is not within quotes
- 00:28:28so this will display the sentence my number is plus our variable
- 00:28:32and in the console window it states my number is 123.
- 00:28:36so with integer variables
- 00:28:37the largest number that you can store within an integer variable
- 00:28:41is just over 2 billion let's say we are working with an extremely large number
- 00:28:45like the amount of student debt that i owe well this number is too large to
- 00:28:50store it within an integer variable we would probably want to use the long
- 00:28:54data type and one convention with assigning values
- 00:28:57to a long variable is that you need to follow this
- 00:29:01number with a capital l for some reason so we
- 00:29:05can now work with extremely large numbers
- 00:29:07so this might be useful if you're working with numbers like the speed of
- 00:29:10light or something so we now have a long variable and we
- 00:29:14can display this value a few of the other data types
- 00:29:18that we mentioned were bytes and shorts they have a lesser
- 00:29:21number that they can store so with bytes you can only store up to i
- 00:29:25believe 127 so we could store like 100 within here
- 00:29:30and this would be fine but 130 would be a little too much though so we don't
- 00:29:35tend to use bytes and shorts too much as a beginner because it's just way more
- 00:29:40convenient to work with integers um but you might use longs every once in
- 00:29:44a while too but as beginners we're mostly going to be
- 00:29:47sticking with integers now a double can store a number with a
- 00:29:53fractional portion with integers we cannot store
- 00:29:55a decimal portion so if this was 123.01 well we cannot store this decimal
- 00:30:02portion we can only do so with a float or a
- 00:30:05double so with a float you would type in float
- 00:30:09for the data type let's create a new variable like
- 00:30:11y float y equals 3.14 and a common convention for assigning
- 00:30:18numbers well values to float variables is that
- 00:30:21you have to follow this with f so you can store
- 00:30:26a number with a decimal portion within a float
- 00:30:29or a double and then we could display whatever this value is
- 00:30:33so y is equal to 3.14 but people tend to use doubles more
- 00:30:38because they have more precision and then you do not need this f at the
- 00:30:42end so this will store up to 15 digits after
- 00:30:47the decimal portion so we also have booleans let's say
- 00:30:51boolean z equals this holds either true
- 00:30:56or false and then we can display what value is within this boolean so if
- 00:31:02we print our variable z this will display false
- 00:31:06or we could hold true and this will display
- 00:31:10true but we can't display anything else besides those two
- 00:31:13like we cannot hold the word pizza because booleans only hold
- 00:31:17true or false we have characters char for short char and we don't
- 00:31:23necessarily need to come up with a variable that's only one
- 00:31:27letter we could have a name or something that's descriptive for this
- 00:31:30let's say we have a variable called symbol
- 00:31:34char symbol equals and then place a character within single quotes let's
- 00:31:40say we want the at sign so we now have
- 00:31:44a variable called symbol that contains the at sign
- 00:31:48so if i were to display this variable symbol to the console window
- 00:31:52it will display the value that is contained within which is the at sign
- 00:31:56and lastly we have strings so with strings these start with a capital s
- 00:32:01because they are of the reference data type anything that's a reference
- 00:32:04data type begins with a capital letter and let's say we want to store our name
- 00:32:10so string is the data type let's say the variable name is name
- 00:32:14equals and to store a string it works similar
- 00:32:17to a string literal we're going to use a set of double
- 00:32:20quotes and display or add a bunch of text like my name
- 00:32:27and then i can now display my name to the console window
- 00:32:31or i could do some string concatenation too and display
- 00:32:35the word hello plus my name and within the console window it's now
- 00:32:39going to display hello bro so that is everything you need to
- 00:32:44know to get started with variables in java if you would like a
- 00:32:48copy of all this code i will post all of this in the comments down
- 00:32:51below don't be afraid to smash that like button
- 00:32:54drop a comment down below and subscribe if you'd like to become a fellow bro
- 00:32:59how's it going everybody it's your bro here hope you're doing well and in this
- 00:33:02video I'm going to teach you guys how we can swap two variables using Java let's
- 00:33:06get into it
- 00:33:09if you find this video helpful please remember to Like comment and subscribe
- 00:33:14your support will help keep this channel running alright well here we got two
- 00:33:19variables we got variable X which contains water and variable Y which I'm
- 00:33:23gonna put kool-aid in more specifically black cherry kool-aid so let's just put
- 00:33:27this in good enough and let's mix it
- 00:33:34perfect so we're going to create two variables and these are going to be of
- 00:33:42the string datatype so we'll have string x equals the word water and string y
- 00:33:51equals the word kool-aid and then let's display these with a simple print line
- 00:33:58statement and within here I'll just type in X colon space plus the variable X and
- 00:34:08then let's do the same thing for y so we'll change X to Y and let's display
- 00:34:13these so X currently has water and Y currently has kool-aid so what happens
- 00:34:19if we set X to equal Y well we get kool-aid for both x and y okay we're
- 00:34:27gonna assign variable Y to variable X shit so yeah it looks like we have
- 00:34:34kool-aid in both variable X and variable Y let's try again maybe we can set Y to
- 00:34:40equal X no we just got water everywhere let's try this again we're gonna assign
- 00:34:46variable X to variable Y this time okay that doesn't seem to work guys my floor
- 00:34:53is getting really sticky so it appears that we're going to have to store one of
- 00:34:58these values within another variable in order to switch these so one thing
- 00:35:01though we can do is introduce another variable and let's call this temp and
- 00:35:05temp is empty let's create another string so we'll create string temp and
- 00:35:09we can either set this to null or can assign no value at all where we
- 00:35:14simply just declared temp a variable so what we're gonna do is actually take X
- 00:35:20and store X whatever value isn't here within temp so we're gonna type temp
- 00:35:26equals x so now X can be filled with something so we're going to fill it with
- 00:35:32variable Y so on our next line we're going to set X to equal Y then we're
- 00:35:42going to take our temp variable and store this within Y and lastly y equals
- 00:35:53temp breaking news ladies and gentlemen we have switched the contents of X and
- 00:35:57white with the help of our variable temp now back to you bro thank you for the
- 00:36:01live update news anchor bro it appears on our
- 00:36:03end that we have also switched the
- 00:36:05contents of X and y as well so in conclusion if the programming language
- 00:36:10you're using doesn't have any direct function to switch to variables you
- 00:36:15could do this manually and you can create another variable such as temp and
- 00:36:19temp is a temporary value to temporarily store one of these values you can set
- 00:36:24temp to equal X or you can do this with Y and then set X to equal Y and then Y
- 00:36:30to equal temp so your assignment for today is to post two variables in the
- 00:36:35comments down below and the code that you used to swap them and that ladies
- 00:36:38and gentlemen is how you can swap two variables
- 00:36:42hey how's it going everybody it's you bro hope you're doing well and in this
- 00:36:45video i'm going to teach you guys how we can accept some user input
- 00:36:48in java so sit back relax and enjoy the show
- 00:36:54make sure you like comment and subscribe one like equals one prayer for the
- 00:36:59youtube algorithm welcome back ladies and gentlemen i'm
- 00:37:03going to explain how we can use a scanner to accept some user input
- 00:37:07the scanner class is found in the java utility package of your library and we
- 00:37:11need to import that before we can use the scanner so outside of the class at
- 00:37:16the top of our program this is what we're going to type import
- 00:37:20java dot util and the name of the class we would like to import which is scanner
- 00:37:27then a semicolon now we can use the scanner class to create
- 00:37:30a scanner object so we're going to be performing a little bit of object
- 00:37:34oriented programming we'll be covering object oriented
- 00:37:37programming in the later part of the series so don't worry
- 00:37:40so repeat after me scanner then we need a name for the scanner
- 00:37:43let's call it scanner a lowercase equals new
- 00:37:47scanner then a set of parentheses and a semicolon
- 00:37:51within the parentheses we're going to type system dot in
- 00:37:55and there we go we have our scanner so we can use the scanner to accept some
- 00:37:59user input so let's let the user know that we would
- 00:38:03like them to type in something maybe a name let's create a prompt that
- 00:38:07will ask somebody for their name so within a print line statement we'll
- 00:38:10type what is your name
- 00:38:16and next what we'll do is take our user input and assign it to
- 00:38:19a variable perhaps a string variable called name
- 00:38:23string name equals and now we're going to use our scanner so we type in
- 00:38:28the name of the scanner dot and to enter a
- 00:38:31line of text we're going to use a certain method of the scanner
- 00:38:35it is the next line method and when we type in user input we type
- 00:38:41it into the console window here at the bottom so let's do something
- 00:38:45with this name maybe display this within a message
- 00:38:50system.out.printline hello plus whatever your name is so let's
- 00:38:55compile and run this what is your name now our program is
- 00:38:59currently paused until we type in some user input and
- 00:39:03then press the enter key when you press enter that's how you
- 00:39:06submit some user input into the console window
- 00:39:09so i'm going to click within my console window and type in something
- 00:39:13i'm going to type in bro and to submit some user input you press the enter key
- 00:39:17and it states hello plus my name which is what i entered
- 00:39:21bro now there's different types of input that we can accept
- 00:39:26this time let's accept only an integer number perhaps we can ask somebody for
- 00:39:31their age so let's write a prompt for that
- 00:39:34system.out.printline how old are you
- 00:39:41and this time we will declare an integer variable
- 00:39:44maybe called age int age equals scanner dot next
- 00:39:52and we are looking for int so we can only accept a whole integer
- 00:39:59u r plus age plus
- 00:40:05years old okay let's try this again what is your
- 00:40:12name bro how old are you let's just say that
- 00:40:15i'm 18. hit enter hello bro you
- 00:40:19are 18 years old so what happens if we do not enter in
- 00:40:24a number so let's try and break this what is your name
- 00:40:27bro how old are you we're not going to enter a number this time
- 00:40:32let's type in the word pizza and see what happens well we encountered
- 00:40:36an exception we encountered an input mismatch
- 00:40:39exception because when our scanner is looking for an integer
- 00:40:43we typed in a string so we need to make sure that the input
- 00:40:47type the data type matches in a future lesson we'll be covering
- 00:40:52exception handling where we can prevent this very thing from happening
- 00:40:55but for now since we're beginners we'll just have to be sure to type in
- 00:40:59the correct data type of the input that our program
- 00:41:02is looking for now there's one more thing that i want to show you guys this
- 00:41:05is a common problem if you use next line after next int or
- 00:41:10anything else that's not next line so let's ask somebody for their favorite
- 00:41:16food this time so let's add that at the end
- 00:41:19system.out.printline what is your favorite
- 00:41:25food then we'll create a string variable called food
- 00:41:30string food equals scanner dot next line and we will display this
- 00:41:39system.out.printline you like plus food
- 00:41:46all right so here are the questions what is your name
- 00:41:49bro how old are you 18. now pay attention to this when i press
- 00:41:54enter when we reached the question on what is your favorite food
- 00:41:58well our program skipped our user input and continued on with the rest of the
- 00:42:02code so it states hello bro you are 18 years
- 00:42:05old but we were not able to input anything for our favorite food
- 00:42:10here's the reason why here's what's going on let's pretend
- 00:42:13that this box is a representation of our scanner and
- 00:42:17we're going to use the next line method of our scanner to read a
- 00:42:21line of text so we type in our name and then press
- 00:42:24the enter key to submit so this backslash n is an escape
- 00:42:28sequence for a new line the next line method will read an entire
- 00:42:31line of text and stop when it reaches a new line
- 00:42:35character so after we call the next line method our
- 00:42:38scanner is going to be empty however if we were to call a different
- 00:42:42method that doesn't read a new line character such as
- 00:42:45next ins so we type in our input such as the number 18
- 00:42:49and then press the enter key that will add a new line character
- 00:42:53so our next int method is only going to read this numeric portion of our scanner
- 00:42:58and then when we submit it well this new line character is still going to be
- 00:43:02within our scanner and if we were to use our scanner again
- 00:43:06and call a different method like next line well our next line method
- 00:43:11thinks that we're at the end because there's this new line character within
- 00:43:15our scanner so we would need some way to clear out that new line
- 00:43:19one easy fix for this is that after you call the
- 00:43:22next and method what we could do is call the next
- 00:43:26line method to clear our scanner so i'm just going to copy this
- 00:43:29portion and paste it we're not really going to do anything with that newline
- 00:43:33character this will just clear the scanner for us
- 00:43:36so now we should be able to answer all three questions
- 00:43:39so type in your name what is your name it is bro
- 00:43:43how old are you let's say i'm 18. what is your favorite food and you can see
- 00:43:47that it paused to this time unlike the first time and my favorite
- 00:43:51food is pizza press enter hello bro you are 18 years
- 00:43:55old you like pizza well then that is one way in which you
- 00:43:59can use a scanner to accept some user input
- 00:44:02scanners are capable of much more you can also use them to read
- 00:44:05the contents of a file and a few other things but we'll learn about those in
- 00:44:09future videos and if you need to use a scanner be sure
- 00:44:12to include this import at the top import java.util.scanner because the
- 00:44:17scanner class is found within the utility package
- 00:44:20and then you'll need to create a scanner object just by following these steps
- 00:44:24scanner you can call it scanner equals new scanner
- 00:44:28well guys and gals that is how scanners work in java before you go
- 00:44:32i would greatly appreciate it if you could do a favor for me and
- 00:44:35like this video leave a comment down below and subscribe if you'd like to
- 00:44:39become a fellow bro
- 00:44:43how's it going everybody it's you bro hope you're doing well and in this video
- 00:44:46i'm going to teach you guys about expressions in java so
- 00:44:49sit back relax and enjoy the show
- 00:44:55you can become a hero and save our channel by smashing that like button
- 00:44:58drop a comment down below and subscribe if you'd like to become a fellow bro
- 00:45:03now an expression is a combination of operands and
- 00:45:07operators an operand is the values variables numbers or different
- 00:45:12quantities that you might see in a program and operators they are
- 00:45:16those arithmetic symbols that you might see
- 00:45:19such as the plus sign for addition subtraction multiplication division
- 00:45:25and then modulus so let's go over a few examples
- 00:45:28just so that we know how they work let's say i have
- 00:45:31an integer variable called friends and i will set this equal to maybe 10.
- 00:45:37so we can change the value of the friends variable by using an arithmetic
- 00:45:41expression so let's say that we make a new friend
- 00:45:44so we're going to add one to my variable of friends to increment
- 00:45:48my variable friends i can just use the plus
- 00:45:50operator and then add a new operand to my variable friends
- 00:45:54so if i would like to assign a new value to my friends variable i'm going to type
- 00:45:59in the name of my variable equals friends plus one because we made
- 00:46:04a new friend and then i will print the value of
- 00:46:07friends so our friends variable now contains 11.
- 00:46:12so we could also subtract take a wild guess as to what's gonna happen
- 00:46:16we just lost a friend let's multiply uh let's multiply our friends by two and
- 00:46:22now we have 20 friends and let's divide
- 00:46:26we're going to divide our friends into two and we have five friends
- 00:46:31now the modulus gives you the remainder of division
- 00:46:34so we have 10 friends what if we had modulus 3
- 00:46:37well that does not divide evenly so we're going to have a remainder of
- 00:46:41one friend it's kind of like in group projects where everybody has to get into
- 00:46:45a group of three and there's always somebody that's left
- 00:46:47over think of it that way although if our group of friends was
- 00:46:52divided into groups of 2 there would be no remainder because 10
- 00:46:56divided by 2 equals 5 evenly and there is no
- 00:46:59remainder so that's all what the modulus is it gives you the remainder of any
- 00:47:03division that occurs now there is a shorthand to increment a
- 00:47:07variable by one so normally the long way of writing this
- 00:47:11out would be type in the name of the variable
- 00:47:13equals because we're going to reassign a value friends
- 00:47:17plus one right well there's a shorthand way of incrementing this
- 00:47:21and that is to use the increment operator which is just
- 00:47:24plus plus and then a semicolon so this will add one
- 00:47:28to a value and now we have 11 friends if you want to decrement that would be
- 00:47:34minus minus and now we have nine friends hold up wait a minute before you go i
- 00:47:40gotta discuss integer division because i forgot to
- 00:47:43talk about it so if we divide a number by an integer
- 00:47:48if there is normally a remainder well our program is going to truncate the
- 00:47:53remainder here's an example let's say we have 10
- 00:47:56friends and we're going to divide our friends
- 00:47:59by three so we're dividing by a whole integer so our result should be
- 00:48:053.33 repeating right wrong it's three that's because with
- 00:48:09integer division we truncate any decimal portion because
- 00:48:13we cannot store it one easy fix for that is we can cast our result as a double
- 00:48:19value or a float as well to cast a value as a different data type
- 00:48:24to the left hand side of our expression we're going to list
- 00:48:27the new data type that we would like to convert this value to
- 00:48:31so we would like to convert our integer as
- 00:48:34a double value because we would like to retain
- 00:48:37that decimal portion of our result however
- 00:48:41our data type friends is an integer so it cannot store a double
- 00:48:45data type so we should probably convert this to a double
- 00:48:49so that we can store this value and now this program
- 00:48:53will successfully store this decimal portion of our expression
- 00:48:57well that's really all you need to know to get started with expressions
- 00:49:00if you would like a copy of all this i will post this in the comments down
- 00:49:04below and if i could ask for a favor of you
- 00:49:06guys just to like this video drop a comment down below and subscribe
- 00:49:11if you'd like to become a fellow bro
- 00:49:13hey what's going on everybody it's your bro hope you're doing well and in this
- 00:49:16video i'm going to teach you guys how we can make a very basic
- 00:49:19gui application in java so sit back relax
- 00:49:23and enjoy the show
- 00:49:27if you find this video helpful please remember to like
- 00:49:30comment and subscribe your support will help keep this channel running
- 00:49:34all right guys and gals i'm going to teach you all how to create a very basic
- 00:49:38gui program gui is an acronym for graphical
- 00:49:42user interface it's a visual program that we can see
- 00:49:45and interact with kind of like this so we'll be using the j option pane
- 00:49:51class and be creating a few message dialog boxes
- 00:49:55before we begin we'll need an import so outside your class let's type this
- 00:49:59import java x dot swing dot j option pin
- 00:50:06so we will be working with the j option pane
- 00:50:09class now what we would like to do is type in some user information into a
- 00:50:14sort of dialog box and we'll store this as a
- 00:50:18string variable called name kind of like what we did on the
- 00:50:21video on user input string name and in order to create an
- 00:50:26input dialog box we're going to type the name of the class j option pane
- 00:50:33dot and there's a few options what we would like is
- 00:50:36show input dialog and you can really just pick any of these
- 00:50:40and we can type in a message so with this message
- 00:50:44let's state enter your name and when we run this
- 00:50:51this is what we have we have the small gui dialog box and we can type in our
- 00:50:55name and submit it but it currently doesn't do anything so
- 00:50:59let's create another message dialog box that will display our name along with a
- 00:51:03message so j option
- 00:51:07pain dot show message dialogue for now you can just type in no
- 00:51:16comma and then a message let's say hello plus
- 00:51:23name and let's try it so our first dialog box is the show input dialog
- 00:51:29message and we can type in our name and submit a
- 00:51:33name and our second box here is a message
- 00:51:36dialog box this just displays some information
- 00:51:39such as a string of text and it says hello bro
- 00:51:43this time let's ask for an edge and store this value within an integer
- 00:51:46variable called age int age equals and we can just copy all of
- 00:51:51this j option pane dot show input dialog
- 00:51:55and the message will be enter your edge there is one issue though
- 00:52:00when you use the show input dialog box it's going to return
- 00:52:03a string and we're attempting to assign the string into an integer
- 00:52:08variable so what we would need to do is convert this
- 00:52:12to an integer and there is actually a method to do that
- 00:52:16so what i'm going to do is use the integer
- 00:52:19wrapper class and use the parseint method
- 00:52:24and within the parenthesis we're going to take all of this
- 00:52:29and copy it within our parseint method and then add a semicolon at the end so
- 00:52:35this will return a string based on what the user types in
- 00:52:39and the parseint method will convert it to an integer that we can store it
- 00:52:42within our integer variable of age and then we can display this so let's
- 00:52:47display this with another message dialog box j option pane dot
- 00:52:51show message dialog null will be the first argument and our
- 00:52:57message will be u r plus age
- 00:53:03plus years old and let's try it
- 00:53:10enter your name bro press okay hello bro then when we click okay again
- 00:53:16it's going to go to our next input dialog box enter your age let's
- 00:53:20say that i'm 18 click ok you are 18 years old now let's
- 00:53:25try this with a double data type
- 00:53:30this time let's create a variable called maybe height
- 00:53:34this will be a double value and the variable name will be height
- 00:53:38so since we're working with double values we're going to change integer to
- 00:53:42double double with a capital d dot parse double and the message will be enter
- 00:53:50your height and our message dialog will be u r height
- 00:54:00and let's say this is in centimeters tall
- 00:54:04so we have to be sure that we're getting the right data type because when you use
- 00:54:07the input dialog box it's going to return a string
- 00:54:11so if you're attempting to assign that string to a certain data type you have
- 00:54:15to convert it to that specific data type so let's try
- 00:54:18this enter your name bro okay hello bro
- 00:54:23what is your age let's say i'm 18 press okay
- 00:54:26you're 18 years old enter your height i don't know what a good height is
- 00:54:30240 centimeters and then click ok you are 240.0 centimeters tall
- 00:54:37in conclusion ladies and gentlemen what we have made is a
- 00:54:40very simple graphical user interface for fun
- 00:54:44this is completely optional at this point but we will be learning more about
- 00:54:47gooeys later in this playlist so if you would like a copy of all this
- 00:54:51code i will post all of this in the comments down below
- 00:54:54and if you could do me a favor down below smash the like button
- 00:54:58drop a comment and subscribe if you'd like to become a fellow bro
- 00:55:02how's it going everybody it's your bro here hope you're doing well and in this
- 00:55:05video i'm going to teach you guys a few useful methods of the math class and at
- 00:55:09the end of this video we're going to create a program
- 00:55:11where we will find the hypotenuse of a triangle so
- 00:55:15sit back relax and enjoy the show make sure you like comment and subscribe
- 00:55:23one like equals one prayer for the youtube algorithm
- 00:55:26alrighty then guys and gals in this video i'm going to demonstrate a few
- 00:55:32useful methods of the math class to begin we'll need maybe two numbers
- 00:55:36let's say that these will be double variables double
- 00:55:39x let's set the sequel to 3.14 and double y we will set this
- 00:55:46to let's say negative 10. all right the first useful method is the max method
- 00:55:51this will find the larger of two numbers so in order to use the
- 00:55:56max method we're going to type math with a capital m
- 00:55:59dot and there's a few recommendations here what we would like is the
- 00:56:04max method that is right here so there's a few to choose from we can
- 00:56:09compare two integers two long values two floats
- 00:56:13and two doubles i'm going to compare the values of
- 00:56:16x and y so i'm going to put these within the parentheses of my max method and
- 00:56:23then we can either display the result or assign this to a new
- 00:56:26variable let's say double z equals math.max and we'll compare
- 00:56:32x and y and assign the larger number to variable z
- 00:56:37and we will display the result system.out.printline
- 00:56:40z and the larger of these two numbers is in fact x which is 3.14
- 00:56:47and there is also a min method which will find
- 00:56:50the lesser of two values which is variable y
- 00:56:53which is negative 10. another useful method
- 00:56:57is the absolute function method it's just abs
- 00:57:01like abs six-pack abs and we'll pass in y and this will display the absolute
- 00:57:08value of y which is 10. so the absolute value
- 00:57:11is basically a number without the negative
- 00:57:14sign i'm not a mathematician so i could be wrong in that definition all
- 00:57:18right we also have the square root function
- 00:57:21that is sqrt square root of y i don't know what the square root of
- 00:57:29negative 10 is but we're about to find out
- 00:57:31uh i don't know i guess that didn't work let's change y to 10.
- 00:57:363.16 blah blah blah blah all right that is the square root
- 00:57:41function we can also round let's round x
- 00:57:48so x rounded is 3.0 on the other hand seal like ceiling this
- 00:57:55will always round up so 3.4 or 3.14 always rounded up
- 00:58:01is four and floor will always round down so 3.14
- 00:58:08always rounded down is 3.0 here's a project that we can work on
- 00:58:14let's create a program that will find the hypotenuse of a
- 00:58:18triangle and we will ask the user for side x and
- 00:58:22side y so to begin let's declare two variables
- 00:58:26double x and double y actually i take that back let's
- 00:58:32declare double z as well because z will be the
- 00:58:36result the hypotenuse we'll also need a scanner
- 00:58:40to accept some user input scanner scanner equals new
- 00:58:45scanner within the parentheses we're going to type
- 00:58:49system.in we'll need an import so include
- 00:58:52this import at the top import java.util.scanner
- 00:58:56we'll need a prompt for side x and side y
- 00:59:01we can do that with a print line statement enter
- 00:59:04side x i can't type today and we will store the
- 00:59:12result within variable x x equals we need some user
- 00:59:17input scanner dot next double because we would
- 00:59:21like a double value let's repeat the process for side y
- 00:59:28enter side y will store the result within variable y now here's the tricky
- 00:59:34part there is a mathematical formula to
- 00:59:37calculate the hypotenuse given two angles well two sides of a triangle
- 00:59:42so this is what we're going to do first we'll multiply
- 00:59:45x times x plus y times y then we need to put this
- 00:59:52within the square root function of the math class math
- 00:59:57make sure it's with a capital m dot sqrt and we are going to take all of this and
- 01:00:05place this within the square root function and we will
- 01:00:09assign this to our variable of z
- 01:00:14so z equals math dot square root x times x plus y
- 01:00:20times y and with a print line statement we can display
- 01:00:25the hypotenuse is
- 01:00:31plus z that should be good oh then it's good practice to close your
- 01:00:37scanner i always forget to do that although it's not necessary
- 01:00:40scanner.close all right let's test this enter side x that is four enter
- 01:00:48side y let's say five the hypotenuse is six point four add some change all right
- 01:00:55so that's just a simple program that you can make using
- 01:00:58a function of the math class so if you would like a copy of this code
- 01:01:02i will post this in the comments down below
- 01:01:04but yeah that's a few useful methods of the math class
- 01:01:08oh and i forgot to tell you guys like comment and subscribe
- 01:01:12how's it going everybody to bro hope you're doing well and in this video i'm
- 01:01:17going to teach you guys how we can generate some random
- 01:01:19values in java so sit back relax and enjoy the show
- 01:01:27you can become a hero and save our channel by smashing that like button
- 01:01:30drop a comment down below and subscribe if you'd like to become a fellow bro
- 01:01:36all right welcome back ladies and gentlemen in this video we're going to
- 01:01:39be generating some random values some random integers doubles and boolean
- 01:01:44values so if you're interested in game design
- 01:01:47at all this video is a must for you so in order to use random values we
- 01:01:52should probably import the random class bond within the
- 01:01:55library so outside of the class this is where
- 01:01:58we're going to type we're going to import java dot util
- 01:02:04dot random semicolon and now we have access to the random class and that
- 01:02:09provides us a few options we need an instance of the random class
- 01:02:13for us to use within the main method we're going to
- 01:02:16create an instance of the random class by typing random with a capital r
- 01:02:20it's the same name as the class random we need a name for this instance
- 01:02:24let's call it random all lower case kind of like what we did with that video on
- 01:02:28scanners random random equals new random that's
- 01:02:32kind of random then a set of parenthesis then a
- 01:02:34semicolon we now have access to this random instance to generate some random
- 01:02:39values for us but there's a disclaimer here these are
- 01:02:42not true random numbers but something called
- 01:02:45pseudorandom numbers which are pretty darn close so i just wanted to give you
- 01:02:49that disclaimer before we got started so you don't call me a liar let's
- 01:02:53generate a random integer and store this within an integer
- 01:02:56variable like into x into x equals and to generate a
- 01:03:02random integer we're going to type the name of our
- 01:03:05random instance random dot next and
- 01:03:09there's also a few others like next boolean next double
- 01:03:13next float i'll get to those later what we would like for now is
- 01:03:17next int and then we will display the result with a print line statement
- 01:03:23we will display the value of x so the results are on
- 01:03:28a scale between i would say just under negative 2 billion to just over
- 01:03:34positive 2 billion so you'll probably want to limit that
- 01:03:38let's pretend that we're going to roll a six sided dice
- 01:03:41so to limit the scale or the size of the random number that will generate
- 01:03:47we can pass in a value to our next int method so within the parentheses of
- 01:03:53our next int method we'll limit the size of the integer
- 01:03:57that's going to generate if we would like a six-sided dice we're
- 01:04:00going to place six here but there's one catch with this
- 01:04:03though this will generate a random number
- 01:04:06between zero and five because computers always start with zero
- 01:04:10and let's see if i can roll a zero nope there we go all right so one way to
- 01:04:16solve this is that if we want the numbers
- 01:04:18one through six we can just add one so now we'll get a random
- 01:04:23number between one and six just like that
- 01:04:26this time let's generate a random double value
- 01:04:29for now i'll turn this line into a comment
- 01:04:32and let's create a new variable called double
- 01:04:35y double y equals random dot next where is it
- 01:04:42double and we will display the value of y next double is going to
- 01:04:49give us a random value between zero and one
- 01:04:53so this probably has some uses for something
- 01:04:56what that is i'm not really sure but hey you know you can do this now
- 01:05:00let's also generate a random boolean value so
- 01:05:03boolean z equals random dot next boolean
- 01:05:11and we will display the value of z so this is going to give us either true
- 01:05:17or false well everybody that's how we can use the random class to generate
- 01:05:22some pseudo-random values for us if you would
- 01:05:25like a copy of this code i will post this in the comments down below
- 01:05:29but yeah that's a few uses of the random class also be sure to
- 01:05:33leave a like drop a random comment down below
- 01:05:37and subscribe if you'd like to become a fellow bro
- Java programming
- JDK
- IDE
- bytecode
- JVM
- beginner guide
- compilation process
- cross-platform
- installation
- tips and tricks