Java Full Course for free ☕

12:00:00
https://www.youtube.com/watch?v=xk4_1vDrzzo

Summary

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.

Timeline

  • 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.

Show more

Mind Map

Video Q&A

  • 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.

View more video summaries

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