0
What is string? A class, object, array? Am confused
5 Answers
+ 6
A Class is a place where we declare methods , constructors , attributes or variables.
Its name starts with First big letter and other letters are small(Camel Case ex, HelloWorld)
A Class can be declared as public that means we can access its public methods and attributes or variables you can declare it as private to that means you can't access anything inside of it.
A Object object is a part of a class so for example we have Class A and the Class A has method hello and method bye and we want to access the method bye so what we need is a object of class A and using a dot as a connecting to the method bye Code Syntax below.
public class A{
public void hello(){
System.out.println("Hello");
}
public void bye(){
System.out.println("Bye");
}
}
Now we have class B that will access the class A method
public class B{
below we have typed the main method so without the main method our code will never get executed or compiled.
public static void main(String [] args){
A a = new A();
a.bye();
}
A is the class
a is the object of the class A
and = new A(); we are telling the compiler that this is a new object
a.bye(); we are accessing the bye method and if we compile class B and run it will show the bye
String
String is a sequence of Characters together that form a word in programming is called datatype.
String has its methods like concat equals toString split trim and many others check the java documentating for String methods
Source:https://docs.oracle.com/javase/7/docs/api/j
Array is a group of elements sorted from o to N-1 what I mean with this I mean that if we have 5 elements to insert in inside array the index will start from 0 to 4 for exaple we have tree letters to insert in array like A,B,C the index of these three elements will be A = 0 , B = 1 , C = 2;
How you declare a Array ?
There are two ways :
way 1 .
String [] characters = {"a","b","c"};
way 2:
String [] characters = new String(3);
characters[0] = "a";
characters[1] = "b"
characters[2] = "c"
+ 2
Object is not datatype in java it is a Reference Type but I was trying to be simple for newbies
0
All of those are data types. A class is a blueprint basically for objects. An object is a data type that is defined with keys and properties that allow them to be accessed by the object that is made using a class. An array is a data type that is a list of a data type separated by commas. Each member of the array can be accessed by its index. A String is a series of characters, like a word, that is enclosed in quotes. See what Prarabdh Garg said about Strings.
P.S. You can make an array of Strings for example.
class:
public class myClass {
string name: "Jack";
int age: 45;
} // This was in C++ but it is just to give you an idea.
array:
int arr[8] = {1,2,3,4,5,6,7,8};
// That was an array of integers. Also, in C++.
string:
String hello = "Hello World!"; // I used Java for this one.
object:
// Using the class we previously made.
myClass obj;
now we can do the following in main.
cout<<obj.name; //Prints value of the name property of the object of the class
// myClass.
I know I used C++ and you were asking for Java, but the data types are the same with small differences in the way they can be manipulated and/or their syntax. Java is similar in declaring data types. The Java course will teach you how to define all of these.
- 1
In Java, 'string' is actually an user defined variable which behaves as a pre-defined function, mostly because of the abstract class 'String' provided with it.
Being an abstract class, we do not need to create a new string object every time, and can use all the functions present in the String class without having to import the class before using it
- 2
String is a class. When you declare a string like
String s = "Hello World";
String is still a class. and s is an object.