Variables in Java - Practice

Neso Academy
9 Dec 201907:07

Summary

TLDRIn this instructional video, the presenter guides viewers through Java programming basics, focusing on variable declaration, initialization, and value assignment. They demonstrate how to declare a string variable, assign it a value, and print it using System.out.println. The lesson also covers variable copying, showing that changes to the original variable do not affect the copied value. Additionally, the presenter explains how to access command-line arguments in Java, illustrating with an example that prints the first argument provided. The video is designed to solidify understanding of Java fundamentals and practical application.

Takeaways

  • 💻 The lecture focuses on practicing Java programming concepts learned in a previous video.
  • 🔑 The importance of declaring variables with a type and a name is emphasized, following the camelCase convention.
  • ⚠️ Java requires variables to be initialized before they can be used, as demonstrated by the error when trying to print an uninitialized variable.
  • 📝 The process of assigning values to variables using the assignment operator '=' is shown.
  • 🔄 The difference between declaring and initializing a variable in one statement is explained.
  • 🔗 The concept of variable copying is introduced, where a new variable is initialized with the value of an existing variable.
  • 🛠️ The script demonstrates how to change the value of a variable and shows that the copy remains unaffected.
  • 🗣️ The use of command-line arguments in Java programs is discussed, with a focus on accessing the first argument using `args[0]`.
  • 📌 The script shows how to print command-line arguments using `System.out.println()` and how to handle additional unused arguments.
  • 🚫 The potential error of trying to access an argument that hasn't been provided is highlighted, emphasizing proper error handling.

Q & A

  • What is the purpose of the lecture in the transcript?

    -The purpose of the lecture is to practice what was learned in the previous video, focusing on working with variables in Java within the IntelliJ IDE.

  • How is a variable of type string declared in Java?

    -A variable of type string is declared by specifying the type 'String', followed by a space, the variable name using camelCase convention, and ending with a semicolon.

  • Why is it necessary to initialize a variable in Java before using it?

    -In Java, variables must be initialized before use because the language requires that a value is placed inside the variable to avoid errors and ensure proper memory allocation.

  • What is the error that occurs if a string variable is declared without initialization?

    -The error that occurs is that the variable has not been initialized, meaning no value has been assigned to it yet, which is required in Java before the variable can be used.

  • How can a string variable be initialized with a value at the time of declaration?

    -A string variable can be initialized by assigning a value to it immediately after the declaration using the assignment operator '='.

  • What is the output when the program is run after initializing the variable 'myName'?

    -The output will be the value assigned to the 'myName' variable, which is printed to the console.

  • What is the difference between declaring a variable and initializing it?

    -Declaring a variable involves specifying its type and name, while initializing a variable involves assigning an initial value to it. Initialization can be done at the time of declaration.

  • Why does creating a variable named 'myNameCopy' and assigning it the value of 'myName' result in a copy of the value?

    -Assigning the value of 'myName' to 'myNameCopy' creates a copy of the value, meaning any changes to 'myName' will not affect 'myNameCopy' as they are separate variables with separate memory allocations.

  • How can command-line arguments be accessed in a Java program?

    -Command-line arguments can be accessed using the 'args' array, where 'args[0]' represents the first argument passed to the program.

  • What happens if a program tries to access an argument that was not provided?

    -Attempting to access an argument that was not provided will result in an ArrayIndexOutOfBoundsException, indicating that the index is out of bounds for the size of the 'args' array.

  • Why is it okay to have unused command-line arguments in a Java program?

    -It is okay to have unused command-line arguments because they do not affect the execution of the program unless specifically accessed and used in the code.

Outlines

00:00

📝 Java Variable Declaration and Initialization

This paragraph explains the process of declaring and initializing variables in Java. The speaker demonstrates how to declare a string variable named 'myName' using the camelCase convention and how to assign a value to it using the assignment operator. They also address the error that occurs if a variable is used before it's initialized and how to resolve it. The paragraph further illustrates the concept of variable initialization at the point of declaration and the difference between copying a variable's value versus redeclaring a variable with the same name.

05:00

🔍 Understanding Variable Copying and Command-Line Arguments

The second paragraph delves into the concept of copying variable values in Java and the handling of command-line arguments. The speaker creates a new variable 'myJob' and assigns it the value 'programmer', then prints it to demonstrate the output. They also create a third variable 'myNameCopy' as a copy of 'myName', explaining that changes to the original variable do not affect the copy. Additionally, the speaker introduces the concept of accessing command-line arguments through the 'args' array, demonstrating how to store and print the first argument passed to the program, while also discussing the implications of unused arguments and the errors that can occur when attempting to access non-existent arguments.

Mindmap

Keywords

💡IntelliJ

IntelliJ is an integrated development environment (IDE) used primarily for Java development. In the video, IntelliJ serves as the platform where the presenter is demonstrating Java coding practices. It is central to the video's theme of programming in Java, as it is the tool used to create, edit, and run the Java code examples.

💡Variable

A variable is a storage location in a computer program where a value or data can be stored and manipulated. In the context of the video, variables are declared and used to store strings such as 'my name' and 'my job'. The video explains the process of declaring, initializing, and using variables in Java, which is fundamental to understanding programming concepts.

💡String

A string in Java is a sequence of characters enclosed in double quotes. The video focuses on string variables, demonstrating how to declare and assign values to them. For example, 'my name' is a string variable that is assigned the value 'my name', illustrating the basic concept of strings in Java programming.

💡CamelCase Convention

The CamelCase convention is a naming convention used in programming where the first letter of each word in a compound word is capitalized, with no spaces or underscores between words. In the video, when the presenter declares the variable 'myName', they follow this convention, which is a standard practice in Java for variable naming.

💡Initialization

Initialization refers to the process of assigning a value to a variable at the time of its declaration. The video demonstrates two methods of initializing a variable: first by declaring and then assigning a value, and second by initializing it in one statement. This concept is crucial for understanding how variables are set up in Java.

💡Assignment Operator

The assignment operator (=) is used to assign a value to a variable. In the video, the presenter uses the assignment operator to set the value of the 'my name' variable to a string. This operator is fundamental in programming for setting the initial or updated values of variables.

💡Error Handling

Error handling in programming involves managing and responding to the errors or exceptions that occur during the execution of a program. The video script mentions an error that occurs when a variable is declared but not initialized, and how correcting this by assigning a value resolves the error, demonstrating basic error handling in Java.

💡Copy

In the context of the video, a copy refers to creating a new variable that holds the same value as another variable. The presenter creates 'myNameCopy' as a copy of 'myName', explaining that changes to the original variable do not affect the copy. This concept is important for understanding variable independence in Java.

💡Command-Line Argument

A command-line argument is a parameter provided to a program when it is executed from the command line. In the video, the presenter demonstrates how to retrieve and use command-line arguments in Java, specifically showing how to store the first argument in a variable named 'arg1', which is used to print the value 'R Sub Zero'.

💡Scope

Scope in programming refers to the visibility and accessibility of variables within different parts of a program. The video mentions an error related to variable scope, where the presenter attempts to redefine a variable 'myName' that already exists in the same scope, illustrating the concept of variable scoping in Java.

💡Print

The print function, specifically 'println' in Java, is used to output data to the console. Throughout the video, the presenter uses 'println' to display the values of variables, demonstrating a basic method of outputting data in Java programming.

Highlights

Introduction to practicing concepts from the previous video in IntelliJ.

Demonstration of closing an unnecessary class in the same project.

Explanation of declaring a variable of type string with camelCase naming convention.

Error handling when trying to print an uninitialized string variable.

Assigning a value to a string variable to resolve the initialization error.

Running the program to print the value of the initialized string variable.

Direct initialization of a string variable with a value upon declaration.

Creating and printing a new string variable with a specific job title.

Creating a copy of a string variable to demonstrate value copying.

Explanation of how changes to the original variable do not affect the copied variable.

Demonstration of changing the value of a string variable and the effect on the copy.

Introduction to creating a variable from command-line arguments.

Storing the first command-line argument in a variable and printing it.

Adding command-line arguments in the IntelliJ run configuration.

Running the program with command-line arguments and printing the first argument.

Handling unused command-line arguments without causing program issues.

Printing all command-line arguments using the args array.

Error handling when attempting to access an out-of-range command-line argument.

Conclusion of the video with an invitation to the next lecture.

Transcripts

play00:00

hello guys and welcome back in this

play00:01

lecture we are going to practice what we

play00:03

learned in the previous video so let's

play00:05

go to IntelliJ and get started as you

play00:07

can see I'm still in the same project I

play00:09

will close this class over here because

play00:11

I'm not going to use it we will work and

play00:13

sort them any class all right

play00:15

so inside our main method let's declare

play00:17

a variable of type string so we will put

play00:20

the type which is string and then we

play00:22

will leave a space and over here we will

play00:24

put the name of the variable so let's

play00:26

call this variable my name like this and

play00:28

remember to use the camelcase convention

play00:30

now of course at the end we'll put a

play00:33

semicolon so this statement over here

play00:35

allocates some space inside our memory

play00:37

this space will be called my name and it

play00:40

can store a string all right so let's

play00:43

say try to print this string so us out

play00:45

and over here I would put the name of

play00:47

the variable which is my name look at

play00:50

this we have an error this error says

play00:52

the variable my name have not been

play00:54

initialized so in Java in order to use

play00:57

variables we have to put a value inside

play00:59

them alright so let's do that after

play01:02

declaring the variable I'm going to put

play01:04

a value inside it and we're going to use

play01:06

the assignment operator

play01:07

so I've loosed the name of the variable

play01:09

and I will set it equal to a string

play01:12

right and this string will be my name in

play01:14

this case and of course we will put a

play01:16

semicolon

play01:17

so in this statement we are assigning a

play01:19

value to our variable and have a look

play01:21

the error over here is gone and this is

play01:23

because now we have a value inside our

play01:26

variable so we are declaring a variable

play01:28

we are storing a value inside it and

play01:30

then we are printing it let's run our

play01:32

program and have a look over here this

play01:34

is our output all right now instead of

play01:37

declaring this variable and then

play01:38

assigning a value let's initialize it

play01:41

right away so what we have to do is the

play01:43

following we are going to declare the

play01:45

variable like this and immediately we

play01:47

will set it equal to this string right

play01:49

so in this statement over here we are

play01:51

initializing our variable let's run the

play01:53

program again and as you can see we have

play01:55

the same result now let's create another

play01:57

variable alright it will be of type

play01:59

string also and it is called my job okay

play02:02

and that's assigned to programmer for

play02:05

example okay now let's see print the new

play02:07

variable so as out and over here we will

play02:10

say my job so run the program and as you

play02:13

can

play02:14

this is our output perfect now I'm going

play02:17

to create a third variable which will be

play02:19

a copy of my name so I'll create a

play02:22

variable of type string and for example

play02:24

I will call it my name copy of course

play02:27

the name can be anything now I'm going

play02:29

to initialize this variable to be equal

play02:32

to my name variable so have a look at

play02:34

this over here I'm going to say my name

play02:36

so what will happen exactly this over

play02:39

here is an expression because it

play02:40

evaluates to a value all right so what

play02:43

is the value that we'll get from this

play02:45

variable it is the value that is inside

play02:47

the variable just like when we are

play02:49

printing the variable we actually print

play02:51

the value inside it right so over here

play02:53

I'm getting the value that is inside

play02:55

this variable and I'm storing it inside

play02:57

my name copy so I'm going to print my

play03:00

name copy over here

play03:01

so as out my name copy and let's run the

play03:04

program and have a look this is a copy

play03:07

of my name variable all right and pay

play03:09

attention as I said this is a copy so

play03:12

for example if I change this variable

play03:14

over here my name copy will not be

play03:16

changed because over here we are copying

play03:19

the value of my name and you are storing

play03:21

a copy of it inside my name copy so the

play03:24

value of this variable and the value of

play03:26

this variable are separate they are

play03:28

different all right so let me give you

play03:30

an example over here I'm going to change

play03:32

the value of my name so simply I'm going

play03:35

to assign it to a new value so I will

play03:37

use the name of the variable which is my

play03:39

name and I will assign it to a new

play03:41

string for example I'm going to put

play03:43

another name okay anything so first of

play03:45

all not that I'm using only the name of

play03:48

the variable

play03:48

I'm not pre declaring it so let me show

play03:51

this I will put that string keyword over

play03:53

here and now I have an error and let's

play03:56

see this error it's saying that the

play03:58

variable my name is already defined in

play04:00

this scope this means that we already

play04:02

have a variable that is called my name

play04:04

and on this statement over here we are

play04:06

read eclair yet or we are redefining it

play04:09

alright so let me remove the string

play04:11

keyword and let's continue so what's

play04:13

happening on this statement we are

play04:15

basically putting the value of this

play04:17

string inside this variable so the

play04:19

previous value which is this one will be

play04:21

replaced and as I said my name copy is a

play04:24

copy of my name variable so my name copy

play04:27

will not be affected let's run the

play04:29

program and see the output

play04:31

have a look over here this is the value

play04:33

of the variable my name so as you can

play04:35

see it is now another name okay and this

play04:38

over here is the value of my name copy

play04:40

so as you can see it was not affected

play04:43

when we changed the value of my name

play04:45

alright so it is a separate value

play04:47

perfect now let me comment this code and

play04:50

I want to show you one more thing so let

play04:52

me create another variable for example I

play04:55

will call it our Guan it is an

play04:57

abbreviation for argument 1 and you can

play05:00

call it whatever you want and I'm going

play05:01

to assign it to R Sub Zero alright so

play05:05

what happens over here first of all we

play05:07

are creating a variable that is called

play05:09

our Guan and the type of this variable

play05:11

is a string and as you know args is a

play05:14

group of strings right and in order to

play05:17

get the first string we use the brackets

play05:19

and then inside it we put the numbers

play05:21

you so over here this is an expression

play05:23

and the value of this expression is the

play05:26

first string which will be passed as a

play05:28

command-line argument right so I'm

play05:31

storing this value inside this variable

play05:33

over here and after that I'm going to

play05:35

print it using the print alarm function

play05:37

so println or Guan

play05:39

all right now let's go to the run tab

play05:41

and then edit configuration and over

play05:44

here let's add a string so we are going

play05:46

to add some arguments for our program so

play05:49

let's say hello for example all right

play05:50

also I'm going to add a second argument

play05:52

for example boy all right now let's

play05:55

press ok and let's run our program so

play05:58

notice that we can see the first

play05:59

argument printed so we have successfully

play06:01

stored the value of the first argument

play06:03

and sought this variable and we printed

play06:06

it all right now you might ask why did I

play06:08

add a second argument I did that to show

play06:11

you that if you give the program some

play06:13

arguments and you didn't use them this

play06:15

will not be a problem so for example

play06:17

this program received two arguments

play06:19

right hello and boy and we only used the

play06:22

hello argument we didn't use the buy

play06:24

argument and you have no problems with

play06:26

our program and of course if you want to

play06:28

print the second argument you can simply

play06:30

use the println method and print args

play06:33

Sabon so let's run the program and now

play06:36

we have hello and boy okay but remember

play06:39

if you try to print the

play06:41

argument which is args sub-2 this will

play06:43

give you a problem because we didn't

play06:45

give the program a third argument all

play06:47

right so let's run the program and as

play06:50

you can see we have an arrow so this is

play06:53

it for this video and I'll see you in

play06:54

the next one

play06:55

[Applause]

play06:58

[Music]

Rate This

5.0 / 5 (0 votes)

الوسوم ذات الصلة
Java TutorialVariable BasicsString TypeInitializationCommand-LineProgrammingMemory AllocationVariable AssignmentError HandlingCode PracticeIntelliJ IDE
هل تحتاج إلى تلخيص باللغة الإنجليزية؟